Java Code Examples for com.fasterxml.jackson.core.JsonParser#getBooleanValue()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getBooleanValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JacksonHelper.java    From Bats with Apache License 2.0 6 votes vote down vote up
public static Object getValueFromFieldType(JsonParser parser, MinorType fieldType) throws IOException {
  switch (fieldType) {
    case BIGINT:
      return parser.getLongValue();
    case VARCHAR:
      return parser.getValueAsString();
    case FLOAT4:
      return parser.getFloatValue();
    case BIT:
      return parser.getBooleanValue();
    case LATE:
    case NULL:
      return null;
    default:
      throw new RuntimeException("Unexpected Field type to return value: " + fieldType.toString());
  }
}
 
Example 2
Source File: MincodeParserSamplesTest.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
private static Object getCurrentValue(final JsonParser parser) throws IOException {
    final Object value;
    switch (parser.getCurrentToken()) {
        case VALUE_STRING:
            value = parser.getText();
            break;
        case VALUE_TRUE:
        case VALUE_FALSE:
            value = parser.getBooleanValue();
            break;
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            value = parser.getDecimalValue();
            break;
        case VALUE_NULL:
        default:
            value = null;
    }
    return value;
}
 
Example 3
Source File: AvroObjectDeserializer.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
Object deserialize(JsonParser p) throws IOException {
  while (p.currentToken() != null) {
    switch (p.currentToken()) {
      case VALUE_STRING:
        return p.getText();
      case VALUE_NUMBER_FLOAT:
        return p.getDoubleValue();
      case VALUE_NUMBER_INT:
        return p.getLongValue();
      case VALUE_FALSE:
      case VALUE_TRUE:
        return p.getBooleanValue();
      case VALUE_NULL:
        return null;
      case START_OBJECT:
        return deserializeObject(p);
      case START_ARRAY:
        return deserializeArray(p);
      case FIELD_NAME:
      case END_OBJECT:
      case END_ARRAY:
        //ignore
        break;
      default:
        throw new IllegalStateException("Unexpected token: " + p.currentToken());
    }
    p.nextToken();
  }
  throw new IllegalStateException("Unexpectedly finished processing stream");
}
 
Example 4
Source File: JsonTerminologyIO.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
private static <T extends Enum<T> & Property<?>> Object readPropertyValue(JsonParser jp, 
		T property) throws IOException {
	if(property.getRange().equals(Double.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_FLOAT);
		return jp.getDoubleValue();
	} else if(property.getRange().equals(Float.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_FLOAT);
		return (float)jp.getDoubleValue();
	} else if(property.getRange().equals(Integer.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_INT);
		return jp.getIntValue();
	} else if(property.getRange().equals(Long.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_NUMBER_INT);
		return jp.getLongValue();
	} else if(property.getRange().equals(Boolean.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_FALSE, JsonToken.VALUE_TRUE);
		return jp.getBooleanValue();
	} else if(property.getRange().equals(String.class)) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_STRING);
		return jp.getValueAsString();
	} else if(Set.class.isAssignableFrom(property.getRange())) {
		checkToken(property, jp.currentToken(), JsonToken.START_ARRAY);
		HashSet<Object> values = new HashSet<>();
		readCollection(jp, values);
		return values;

	} else if(property.getRange().isEnum()) {
		checkToken(property, jp.currentToken(), JsonToken.VALUE_STRING);
		PropertyValue theValue;
		String jsonString = jp.getValueAsString();
		theValue = loadEnumConstant(property, jsonString);
		return (Comparable<?>) theValue;
	} else {
		throw new UnsupportedOperationException(String.format(
				"Unsupported property range <%s> in property %s",property.getRange(), property));
	}
}
 
Example 5
Source File: OpenGLSettingsBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public OpenGLSettings parseOnJackson(JsonParser jacksonParser) throws Exception {
  OpenGLSettings instance = new OpenGLSettings();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "openGLAsyncMode":
          // field asyncMode (mapped with "openGLAsyncMode")
          instance.asyncMode=jacksonParser.getBooleanValue();
        break;
        case "openGLDebug":
          // field debug (mapped with "openGLDebug")
          instance.debug=jacksonParser.getBooleanValue();
        break;
        case "openGLMaxFPS":
          // field maxFPS (mapped with "openGLMaxFPS")
          instance.maxFPS=jacksonParser.getIntValue();
        break;
        case "openGLSafeMode":
          // field safeMode (mapped with "openGLSafeMode")
          instance.safeMode=jacksonParser.getBooleanValue();
        break;
        case "openGLVersion":
          // field version (mapped with "openGLVersion")
          instance.version=jacksonParser.getIntValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 6
Source File: ApplicationSettingsBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public ApplicationSettings parseOnJackson(JsonParser jacksonParser) throws Exception {
  ApplicationSettings instance = new ApplicationSettings();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "applicationActivityClazz":
          // field activityClazz (mapped with "applicationActivityClazz")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.activityClazz=jacksonParser.getText();
          }
        break;
        case "applicationClazz":
          // field clazz (mapped with "applicationClazz")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.clazz=jacksonParser.getText();
          }
        break;
        case "applicationConfigClazz":
          // field configClazz (mapped with "applicationConfigClazz")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.configClazz=jacksonParser.getText();
          }
        break;
        case "applicationGestureListenerClazz":
          // field gestureListenerClazz (mapped with "applicationGestureListenerClazz")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.gestureListenerClazz=jacksonParser.getText();
          }
        break;
        case "applicationMode":
          // field mode (mapped with "applicationMode")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            String tempEnum=jacksonParser.getText();
            instance.mode=StringUtils.hasText(tempEnum)?ModeType.valueOf(tempEnum):null;
          }
        break;
        case "applicationResetConfig":
          // field resetConfig (mapped with "applicationResetConfig")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.resetConfig=jacksonParser.getBooleanValue();
          }
        break;
        case "applicationSplashScreenTimeout":
          // field splashScreenTimeout (mapped with "applicationSplashScreenTimeout")
          instance.splashScreenTimeout=jacksonParser.getIntValue();
        break;
        case "applicationStartupTaskClazz":
          // field startupTaskClazz (mapped with "applicationStartupTaskClazz")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.startupTaskClazz=jacksonParser.getText();
          }
        break;
        case "applicationUpgradePolicyClazz":
          // field upgradePolicyClazz (mapped with "applicationUpgradePolicyClazz")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.upgradePolicyClazz=jacksonParser.getText();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 7
Source File: PrefixConfigBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public PrefixConfig parseOnJackson(JsonParser jacksonParser) throws Exception {
  PrefixConfig instance = new PrefixConfig();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "defaultCountry":
          // field defaultCountry (mapped with "defaultCountry")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.defaultCountry=jacksonParser.getText();
          }
        break;
        case "dialogTimeout":
          // field dialogTimeout (mapped with "dialogTimeout")
          instance.dialogTimeout=jacksonParser.getLongValue();
        break;
        case "dualBillingPrefix":
          // field dualBillingPrefix (mapped with "dualBillingPrefix")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.dualBillingPrefix=jacksonParser.getText();
          }
        break;
        case "enabled":
          // field enabled (mapped with "enabled")
          instance.enabled=jacksonParser.getBooleanValue();
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 8
Source File: PrefixConfigBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public PrefixConfig parseOnJackson(JsonParser jacksonParser) throws Exception {
  PrefixConfig instance = new PrefixConfig();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "defaultCountry":
          // field defaultCountry (mapped with "defaultCountry")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.defaultCountry=jacksonParser.getText();
          }
        break;
        case "dialogTimeout":
          // field dialogTimeout (mapped with "dialogTimeout")
          instance.dialogTimeout=jacksonParser.getLongValue();
        break;
        case "dualBillingPrefix":
          // field dualBillingPrefix (mapped with "dualBillingPrefix")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.dualBillingPrefix=jacksonParser.getText();
          }
        break;
        case "enabled":
          // field enabled (mapped with "enabled")
          instance.enabled=jacksonParser.getBooleanValue();
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 9
Source File: PrefixConfigBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public PrefixConfig parseOnJackson(JsonParser jacksonParser) throws Exception {
  PrefixConfig instance = new PrefixConfig();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "defaultCountry":
          // field defaultCountry (mapped with "defaultCountry")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.defaultCountry=jacksonParser.getText();
          }
        break;
        case "dialogTimeout":
          // field dialogTimeout (mapped with "dialogTimeout")
          instance.dialogTimeout=jacksonParser.getLongValue();
        break;
        case "dualBillingPrefix":
          // field dualBillingPrefix (mapped with "dualBillingPrefix")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.dualBillingPrefix=jacksonParser.getText();
          }
        break;
        case "enabled":
          // field enabled (mapped with "enabled")
          instance.enabled=jacksonParser.getBooleanValue();
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 10
Source File: PrefixConfigBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public PrefixConfig parseOnJackson(JsonParser jacksonParser) throws Exception {
  PrefixConfig instance = new PrefixConfig();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "defaultCountry":
          // field defaultCountry (mapped with "defaultCountry")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.defaultCountry=jacksonParser.getText();
          }
        break;
        case "dialogTimeout":
          // field dialogTimeout (mapped with "dialogTimeout")
          instance.dialogTimeout=jacksonParser.getLongValue();
        break;
        case "dualBillingPrefix":
          // field dualBillingPrefix (mapped with "dualBillingPrefix")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.dualBillingPrefix=jacksonParser.getText();
          }
        break;
        case "enabled":
          // field enabled (mapped with "enabled")
          instance.enabled=jacksonParser.getBooleanValue();
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 11
Source File: PrefixConfigBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public PrefixConfig parseOnJackson(JsonParser jacksonParser) throws Exception {
  PrefixConfig instance = new PrefixConfig();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "defaultCountry":
          // field defaultCountry (mapped with "defaultCountry")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.defaultCountry=jacksonParser.getText();
          }
        break;
        case "dialogTimeout":
          // field dialogTimeout (mapped with "dialogTimeout")
          instance.dialogTimeout=jacksonParser.getLongValue();
        break;
        case "dualBillingPrefix":
          // field dualBillingPrefix (mapped with "dualBillingPrefix")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.dualBillingPrefix=jacksonParser.getText();
          }
        break;
        case "enabled":
          // field enabled (mapped with "enabled")
          instance.enabled=jacksonParser.getBooleanValue();
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 12
Source File: TodoBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Todo parseOnJackson(JsonParser jacksonParser) throws Exception {
  Todo instance = new Todo();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "completed":
          // field completed (mapped with "completed")
          instance.completed=jacksonParser.getBooleanValue();
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        case "title":
          // field title (mapped with "title")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.title=jacksonParser.getText();
          }
        break;
        case "userId":
          // field userId (mapped with "userId")
          instance.userId=jacksonParser.getLongValue();
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 13
Source File: StoneSerializers.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public Boolean deserialize(JsonParser p) throws IOException, JsonParseException {
    Boolean value = p.getBooleanValue();
    p.nextToken();
    return value;
}
 
Example 14
Source File: JsonStreamDocumentReader.java    From ojai with Apache License 2.0 4 votes vote down vote up
/**
 * Reads and caches the current Value from the stream.
 * The parser must be positioned at the value and the
 * current {@link EventType} must be set by calling
 * {@link #setCurrentEventType(EventType)}.
 */
protected void cacheCurrentValue() {
  try {
    final JsonParser parser = getParser();
    switch (currentEvent) {
    case BOOLEAN:
      currentObjValue = isEventBoolean() ? parser.getBooleanValue()
          : Boolean.valueOf(getValueAsString());
      break;
    case STRING:
      currentObjValue = parser.getText();
      break;
    case BYTE:
      currentLongValue = getValueAsLong() & 0xff;
      break;
    case SHORT:
      currentLongValue = getValueAsLong() & 0xffff;
      break;
    case INT:
      currentLongValue = getValueAsLong() & 0xffffffff;
      break;
    case LONG:
      currentLongValue = getValueAsLong();
      break;
    case FLOAT:
    case DOUBLE:
      currentDoubleValue = getValueAsDouble();
      break;
    case DECIMAL:
      currentObjValue = Values.parseBigDecimal(parser.getText());
      break;
    case DATE:
      currentObjValue = ODate.parse(parser.getText());
      break;
    case TIME:
      currentObjValue = OTime.parse(parser.getText());
      break;
    case TIMESTAMP:
      currentObjValue = OTimestamp.parse(parser.getText());
      break;
    case INTERVAL:
      currentLongValue = getValueAsLong();
      break;
    case BINARY:
      try {
        final String base64String = parser.getText();
        final byte[] value = CODEC.decode(base64String);
        currentObjValue = ByteBuffer.wrap(value);
      } catch (JsonParseException | IllegalArgumentException e) {
        throw new DecodingException("Unable to decode base64 encoded binary value: " + e.getMessage(), e);
      }
      break;
    default:
      // ARRAY, MAP and NULL need not be cached
      break;
    }
  } catch (IOException ie) {
    throw new DecodingException(ie);
  }
}
 
Example 15
Source File: PrimitiveKVHandler.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
public boolean value(DeserializationContext ctx, JsonParser parser) throws IOException {
    return parser.getBooleanValue();
}
 
Example 16
Source File: ObjectValueDeserializer.java    From smartsheet-java-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public ObjectValue deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectValue objectValue;
    ContactObjectValue contactObjectValue = null;

    if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        ObjectValueAttributeSuperset superset = mapper.readValue(jp, ObjectValueAttributeSuperset.class);

        ObjectValueType parsedObjectType;
        try {
            parsedObjectType = ObjectValueType.valueOf(superset.objectType);
        } catch (IllegalArgumentException e) {
            // If a new object type is introduced to the Smartsheet API that this version of the SDK doesn't support,
            // return null instead of throwing an exception.
            return null;
        }

        switch (parsedObjectType) {
            case DURATION:
                objectValue = new Duration(
                        superset.negative,
                        superset.elapsed,
                        superset.weeks,
                        superset.days,
                        superset.hours,
                        superset.minutes,
                        superset.seconds,
                        superset.milliseconds);
                break;

            case PREDECESSOR_LIST:
                objectValue = new PredecessorList(superset.predecessors);
                break;

            case CONTACT:
                contactObjectValue = new ContactObjectValue();
                contactObjectValue.setName(superset.name);
                contactObjectValue.setEmail(superset.email);
                contactObjectValue.setId(superset.id);
                objectValue = contactObjectValue;
                break;

            case DATE:                // Intentional fallthrough
            case DATETIME:            // Intentional fallthrough
            case ABSTRACT_DATETIME:
                objectValue = new DateObjectValue(parsedObjectType, superset.value);
                break;

            case MULTI_CONTACT:
                List<ContactObjectValue> contactObjectValues = new ArrayList<ContactObjectValue>();
                for(Object contact: superset.values) {
                    contactObjectValue = mapper.convertValue(contact, ContactObjectValue.class);
                    contactObjectValues.add(contactObjectValue);
                }
                objectValue = new MultiContactObjectValue(contactObjectValues);
                break;

            case MULTI_PICKLIST:
                objectValue = new MultiPicklistObjectValue((List<String>)superset.values);
                break;

            default:
                objectValue = null;
        }
    } else {
        JsonToken token = jp.getCurrentToken();
        if (token.isBoolean()) {
            objectValue = new BooleanObjectValue(jp.getBooleanValue());
        } else if (token.isNumeric()) {
            objectValue = new NumberObjectValue(jp.getNumberValue());
        } else {
            objectValue = new StringObjectValue(jp.getText());
        }
    }
    return objectValue;
}
 
Example 17
Source File: BooleanToShortJSONDeserializer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Short deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
	Short truthy = 1;
	Short falsy = 0;
	return parser.getBooleanValue() ? truthy : falsy;
}
 
Example 18
Source File: BooleanJsonDeserializer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Integer deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
	return parser.getBooleanValue() ? 1 : 0;
}
 
Example 19
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private Object handlePrimitive(JsonParser parser, FieldDescriptor field) throws IOException {
    Object value = null;

    JsonToken token = parser.getCurrentToken();

    if (token.equals(JsonToken.VALUE_NULL)) {
        return value;
    }

    switch (field.getType()) {
        case INT32:
        case SINT32:
        case SFIXED32:
        	value = parser.getIntValue();
            break;

        case INT64:
        case SINT64:
        case SFIXED64:
        	value = parser.getLongValue();
            break;

        case UINT32:
        case FIXED32:
        	long valueLong = parser.getLongValue();
        	if (valueLong < 0 || valueLong > MAX_UINT_VALUE) {
        		throw new NumberFormatException("Number must be positive: " + valueLong);
        	}
        	value = (int) valueLong;
            break;

        case UINT64:
        case FIXED64:
        	BigInteger valueBigInt = parser.getBigIntegerValue();
            // valueBigInt < 0 || valueBigInt > MAX_ULONG_VALUE
        	if (valueBigInt.compareTo(BigInteger.ZERO) == -1 || valueBigInt.compareTo(MAX_ULONG_VALUE) == 1) {
        		throw new NumberFormatException("Number must be positive: " + valueBigInt);
        	}
        	value = valueBigInt.longValue();
            break;

        case FLOAT:
        	value = parser.getFloatValue();
            break;

        case DOUBLE:
        	value = parser.getDoubleValue();
            break;

        case BOOL:
        	value = parser.getBooleanValue();
            break;

        case STRING:
        	value = parser.getText();
            break;

        case BYTES:
        	value = ByteString.copyFrom(parser.getBinaryValue());
            break;

        case ENUM: {
            EnumDescriptor enumType = field.getEnumType();
            if (token.equals(JsonToken.VALUE_NUMBER_INT)) {
                int number = parser.getIntValue();
                value = enumType.findValueByNumber(number);
                if (value == null) {
                    throw new RuntimeException("Enum type \""
                    		+ enumType.getFullName()
                    		+ "\" has no value with number "
                    		+ number + ".");
                }
            } else {
                String id = parser.getText();
                value = enumType.findValueByName(id);
                if (value == null) {
                	throw new RuntimeException("Enum type \""
                			+ enumType.getFullName()
                			+ "\" has no value named \""
                			+ id + "\".");
                }
            }
            break;
        }

        case MESSAGE:
        case GROUP:
            throw new RuntimeException("Can't get here.");
    }
    return value;
}
 
Example 20
Source File: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
private Object readObject(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {

        MarshallingType<?> type = field.marshallingType();
        switch (p.currentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE: {
                if (type.equals(MarshallingType.BOOLEAN)) {
                    return p.getBooleanValue();
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got boolean field value");
            }

            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT: {
                if (type.equals(MarshallingType.INTEGER)) {
                    return p.getIntValue();
                } else if (type.equals(MarshallingType.LONG)) {
                    return p.getLongValue();
                } else if (type.equals(MarshallingType.FLOAT)) {
                    return p.getFloatValue(); // coerce should work
                } else if (type.equals(MarshallingType.DOUBLE)) {
                    return p.getDoubleValue(); // coerce should work
                } else if (type.equals(MarshallingType.BIG_DECIMAL)) {
                    return p.getDecimalValue(); // coerce should work
                } else if (type.equals(MarshallingType.INSTANT)) { // we serialize as BigDecimals
                    JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(Instant.class));
                    return deser.deserialize(p, ctxt);
                }
                throw new JsonMappingException(p,
                                               "Type mismatch, expecting " + type + " got int/float/double/big_decimal/instant");
            }

            case VALUE_STRING: {
                if (type.equals(MarshallingType.STRING)) {
                    return p.getText();
                } else if (type.equals(MarshallingType.SDK_BYTES)) {
                    byte[] bytes = p.getBinaryValue();
                    return SdkBytes.fromByteArray(bytes);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got string/bytes");
            }

            case START_OBJECT: {
                if (type.equals(MarshallingType.MAP)) {
                    return readMap(field, p, ctxt);
                } else if (type.equals(MarshallingType.SDK_POJO)) {
                    return readPojo(field.constructor().get(), p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo");
            }

            case START_ARRAY: {
                if (type.equals(MarshallingType.LIST)) {
                    return readList(field, p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got List type");
            }

            case VALUE_NULL:
                return null;

            default:
                throw new JsonMappingException(p, "Can not map type " + type + " Token = " + p.currentToken());
        }
    }