in odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonPropertyEntityProducer.java [70:134]
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;
}
}
}