Java Code Examples for org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind#valueOf()

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind#valueOf() . 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: JsonPropertyEntityProducer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected static void appendPropertyValue(final JsonStreamWriter jsonStreamWriter,
                                          final EntityPropertyInfo propertyInfo, final Object value,
                                          boolean validatingFacets,
                                          boolean isDataBasedPropertySerialization) throws IOException, EdmException,
    EntityProviderException {
  if (propertyInfo.isComplex()) {
    if (value == null || value instanceof Map<?, ?>) {
      jsonStreamWriter.beginObject();
      appendPropertyMetadata(jsonStreamWriter, propertyInfo.getType());
      if (value == null && isDataBasedPropertySerialization) {
        jsonStreamWriter.endObject();
        return;
      }
      for (final EntityPropertyInfo childPropertyInfo : ((EntityComplexPropertyInfo) propertyInfo)
          .getPropertyInfos()) {
        final String name = childPropertyInfo.getName();
        if (isDataBasedPropertySerialization && !((Map<?,?>)value).containsKey(name)) {
          continue;
        } 
        jsonStreamWriter.separator();
        jsonStreamWriter.name(name);
        appendPropertyValue(jsonStreamWriter, childPropertyInfo,
            value == null ? null : ((Map<?, ?>) value).get(name), validatingFacets, isDataBasedPropertySerialization);
      }
      jsonStreamWriter.endObject();
    } else {
      throw new EntityProviderProducerException(EntityProviderException.ILLEGAL_ARGUMENT
          .addContent("A complex property must have a Map as data"));
    }
  } else {
    final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
    final Object contentValue = value instanceof Map ? ((Map<?, ?>) value).get(propertyInfo.getName()) : value;
    final EdmFacets facets = validatingFacets ? propertyInfo.getFacets(): null;
    String valueAsString = null;
    try {
    valueAsString = type.valueToString(contentValue, EdmLiteralKind.JSON, facets);
    } catch (EdmSimpleTypeException e) {
      throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
          e.getMessageReference()).updateContent(e.getMessageReference().getContent(), 
              propertyInfo.getName()), e);
    }
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case String:
      jsonStreamWriter.stringValue(valueAsString);
      break;
    case Boolean:
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      jsonStreamWriter.unquotedValue(valueAsString);
      break;
    case DateTime:
    case DateTimeOffset:
      // Although JSON escaping is (and should be) done in the JSON
      // serializer, we backslash-escape the forward slash here explicitly
      // because it is not required to escape it in JSON but in OData.
      jsonStreamWriter.stringValueRaw(valueAsString == null ? null : valueAsString.replace("/", "\\/"));
      break;
    default:
      jsonStreamWriter.stringValueRaw(valueAsString);
      break;
    }
  }
}
 
Example 2
Source File: JsonPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 3
Source File: JsonPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final DeserializerProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 4
Source File: JsonPropertyEntitySerializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected static void appendPropertyValue(final JsonStreamWriter jsonStreamWriter,
                                          final EntityPropertyInfo propertyInfo, final Object value,
                                          boolean validatingFacets) throws IOException, EdmException,
    EntityProviderException { //NOSONAR
  if (propertyInfo.isComplex()) {
    if (value == null || value instanceof Map<?,?>) {
      jsonStreamWriter.beginObject();
      appendPropertyMetadata(jsonStreamWriter, propertyInfo.getType());
      if (value == null) {
        jsonStreamWriter.endObject();
        return;
      }
      for (final EntityPropertyInfo childPropertyInfo : ((EntityComplexPropertyInfo) propertyInfo)
          .getPropertyInfos()) {
        final String name = childPropertyInfo.getName();
        if ( !((Map<?,?>)value).containsKey(name)) { //NOSONAR
          continue;
        } 
        jsonStreamWriter.separator();
        jsonStreamWriter.name(name);
        appendPropertyValue(jsonStreamWriter, childPropertyInfo,
            value == null ? null : ((Map<?,?>) value).get(name), validatingFacets); //NOSONAR
      }
      jsonStreamWriter.endObject();
    } else {
      throw new EntityProviderProducerException(EntityProviderException.ILLEGAL_ARGUMENT
          .addContent("A complex property must have a Map as data"));
    }
  } else {
    final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
    final Object contentValue = value instanceof Entity ? ((Entity) value).
        getProperties().get(propertyInfo.getName()) : value;
    final EdmFacets facets = validatingFacets ? propertyInfo.getFacets(): null;
    String valueAsString = null;
    try {
    valueAsString = type.valueToString(contentValue, EdmLiteralKind.JSON, facets);
    } catch (EdmSimpleTypeException e) {
      throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
          e.getMessageReference()).updateContent(e.getMessageReference().getContent(), 
              propertyInfo.getName()), e);
    }
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case String:
      jsonStreamWriter.stringValue(valueAsString);
      break;
    case Boolean:
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      jsonStreamWriter.unquotedValue(valueAsString);
      break;
    case DateTime:
    case DateTimeOffset:
      // Although JSON escaping is (and should be) done in the JSON
      // serializer, we backslash-escape the forward slash here explicitly
      // because it is not required to escape it in JSON but in OData.
      jsonStreamWriter.stringValueRaw(valueAsString == null ? null : valueAsString.replace("/", "\\/"));
      break;
    default:
      jsonStreamWriter.stringValueRaw(valueAsString);
      break;
    }
  }
}