You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/06/13 12:58:34 UTC
[14/21] [OLINGO-317] Rename and move of some packages and classes
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeserializer.java
new file mode 100755
index 0000000..bb2e0d2
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeserializer.java
@@ -0,0 +1,474 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.AbstractMap.SimpleEntry;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotatable;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.CollectionValue;
+import org.apache.olingo.commons.api.data.ComplexValue;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntitySet;
+import org.apache.olingo.commons.api.data.Linked;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.data.Valuable;
+import org.apache.olingo.commons.api.data.Value;
+import org.apache.olingo.commons.api.domain.ODataError;
+import org.apache.olingo.commons.api.domain.ODataLinkType;
+import org.apache.olingo.commons.api.domain.ODataPropertyType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.serialization.ODataDeserializer;
+import org.apache.olingo.commons.api.serialization.ODataDeserializerException;
+import org.apache.olingo.commons.core.data.AnnotationImpl;
+import org.apache.olingo.commons.core.data.CollectionValueImpl;
+import org.apache.olingo.commons.core.data.ComplexValueImpl;
+import org.apache.olingo.commons.core.data.EntitySetImpl;
+import org.apache.olingo.commons.core.data.EnumValueImpl;
+import org.apache.olingo.commons.core.data.GeospatialValueImpl;
+import org.apache.olingo.commons.core.data.LinkImpl;
+import org.apache.olingo.commons.core.data.LinkedComplexValueImpl;
+import org.apache.olingo.commons.core.data.NullValueImpl;
+import org.apache.olingo.commons.core.data.PrimitiveValueImpl;
+import org.apache.olingo.commons.core.data.PropertyImpl;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.ObjectCodec;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class JsonDeserializer implements ODataDeserializer {
+
+ protected final Pattern CUSTOM_ANNOTATION = Pattern.compile("(.+)@(.+)\\.(.+)");
+ protected final ODataServiceVersion version;
+ protected final boolean serverMode;
+
+ protected String jsonType;
+ protected String jsonId;
+ protected String jsonETag;
+ protected String jsonReadLink;
+ protected String jsonEditLink;
+ protected String jsonMediaEditLink;
+ protected String jsonMediaReadLink;
+ protected String jsonMediaContentType;
+ protected String jsonMediaETag;
+ protected String jsonAssociationLink;
+ protected String jsonNavigationLink;
+ protected String jsonCount;
+ protected String jsonNextLink;
+ protected String jsonDeltaLink;
+ protected String jsonError;
+
+ private JsonGeoValueDeserializer geoDeserializer;
+
+ private JsonParser parser;
+
+ public JsonDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ this.version = version;
+ this.serverMode = serverMode;
+
+ jsonType = version.getJSONMap().get(ODataServiceVersion.JSON_TYPE);
+ jsonId = version.getJSONMap().get(ODataServiceVersion.JSON_ID);
+ jsonETag = version.getJSONMap().get(ODataServiceVersion.JSON_ETAG);
+ jsonReadLink = version.getJSONMap().get(ODataServiceVersion.JSON_READ_LINK);
+ jsonEditLink = version.getJSONMap().get(ODataServiceVersion.JSON_EDIT_LINK);
+ jsonMediaReadLink = version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAREAD_LINK);
+ jsonMediaEditLink = version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAEDIT_LINK);
+ jsonMediaContentType = version.getJSONMap().get(ODataServiceVersion.JSON_MEDIA_CONTENT_TYPE);
+ jsonMediaETag = version.getJSONMap().get(ODataServiceVersion.JSON_MEDIA_ETAG);
+ jsonAssociationLink = version.getJSONMap().get(ODataServiceVersion.JSON_ASSOCIATION_LINK);
+ jsonNavigationLink = version.getJSONMap().get(ODataServiceVersion.JSON_NAVIGATION_LINK);
+ jsonCount = version.getJSONMap().get(ODataServiceVersion.JSON_COUNT);
+ jsonNextLink = version.getJSONMap().get(ODataServiceVersion.JSON_NEXT_LINK);
+ jsonDeltaLink = version.getJSONMap().get(ODataServiceVersion.JSON_DELTA_LINK);
+ jsonError = version.getJSONMap().get(ODataServiceVersion.JSON_ERROR);
+}
+
+ private JsonGeoValueDeserializer getGeoDeserializer() {
+ if (geoDeserializer == null) {
+ geoDeserializer = new JsonGeoValueDeserializer(version);
+ }
+ return geoDeserializer;
+ }
+
+ protected String getJSONAnnotation(final String string) {
+ return StringUtils.prependIfMissing(string, "@");
+ }
+
+ protected String getTitle(final Map.Entry<String, JsonNode> entry) {
+ return entry.getKey().substring(0, entry.getKey().indexOf('@'));
+ }
+
+ protected String setInline(final String name, final String suffix, final JsonNode tree,
+ final ObjectCodec codec, final LinkImpl link) throws IOException {
+
+ final String entityNamePrefix = name.substring(0, name.indexOf(suffix));
+ if (tree.has(entityNamePrefix)) {
+ final JsonNode inline = tree.path(entityNamePrefix);
+ JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(version, serverMode);
+
+ if (inline instanceof ObjectNode) {
+ link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
+ link.setInlineEntity(entityDeserializer.doDeserialize(inline.traverse(codec)).getPayload());
+
+ } else if (inline instanceof ArrayNode) {
+ link.setType(ODataLinkType.ENTITY_SET_NAVIGATION.toString());
+
+ EntitySet entitySet = new EntitySetImpl();
+ Iterator<JsonNode> entries = ((ArrayNode) inline).elements();
+ while (entries.hasNext()) {
+ entitySet.getEntities().add(
+ entityDeserializer.doDeserialize(entries.next().traverse(codec)).getPayload());
+ }
+
+ link.setInlineEntitySet(entitySet);
+ }
+ }
+ return entityNamePrefix;
+ }
+
+ protected void links(final Map.Entry<String, JsonNode> field, final Linked linked, final Set<String> toRemove,
+ final JsonNode tree, final ObjectCodec codec) throws IOException {
+ if (serverMode) {
+ serverLinks(field, linked, toRemove, tree, codec);
+ } else {
+ clientLinks(field, linked, toRemove, tree, codec);
+ }
+ }
+
+ private void clientLinks(final Map.Entry<String, JsonNode> field, final Linked linked, final Set<String> toRemove,
+ final JsonNode tree, final ObjectCodec codec) throws IOException {
+
+ if (field.getKey().endsWith(jsonNavigationLink)) {
+ final LinkImpl link = new LinkImpl();
+ link.setTitle(getTitle(field));
+ link.setRel(version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL) + getTitle(field));
+
+ if (field.getValue().isValueNode()) {
+ link.setHref(field.getValue().textValue());
+ link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
+ }
+
+ linked.getNavigationLinks().add(link);
+
+ toRemove.add(field.getKey());
+ toRemove.add(setInline(field.getKey(), jsonNavigationLink, tree, codec, link));
+ } else if (field.getKey().endsWith(jsonAssociationLink)) {
+ final LinkImpl link = new LinkImpl();
+ link.setTitle(getTitle(field));
+ link.setRel(version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL) + getTitle(field));
+ link.setHref(field.getValue().textValue());
+ link.setType(ODataLinkType.ASSOCIATION.toString());
+ linked.getAssociationLinks().add(link);
+
+ toRemove.add(field.getKey());
+ }
+ }
+
+ private void serverLinks(final Map.Entry<String, JsonNode> field, final Linked linked, final Set<String> toRemove,
+ final JsonNode tree, final ObjectCodec codec) throws IOException {
+
+ if (field.getKey().endsWith(Constants.JSON_BIND_LINK_SUFFIX)
+ || field.getKey().endsWith(jsonNavigationLink)) {
+
+ if (field.getValue().isValueNode()) {
+ final String suffix = field.getKey().replaceAll("^.*@", "@");
+
+ final LinkImpl link = new LinkImpl();
+ link.setTitle(getTitle(field));
+ link.setRel(version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL) + getTitle(field));
+ link.setHref(field.getValue().textValue());
+ link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
+ linked.getNavigationLinks().add(link);
+
+ toRemove.add(setInline(field.getKey(), suffix, tree, codec, link));
+ } else if (field.getValue().isArray()) {
+ for (final Iterator<JsonNode> itor = field.getValue().elements(); itor.hasNext();) {
+ final JsonNode node = itor.next();
+
+ final LinkImpl link = new LinkImpl();
+ link.setTitle(getTitle(field));
+ link.setRel(version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL) + getTitle(field));
+ link.setHref(node.asText());
+ link.setType(ODataLinkType.ENTITY_SET_NAVIGATION.toString());
+ linked.getNavigationLinks().add(link);
+ toRemove.add(setInline(field.getKey(), Constants.JSON_BIND_LINK_SUFFIX, tree, codec, link));
+ }
+ }
+ toRemove.add(field.getKey());
+ }
+ }
+
+ private Map.Entry<ODataPropertyType, EdmTypeInfo> guessPropertyType(final JsonNode node) {
+ ODataPropertyType type;
+ EdmTypeInfo typeInfo = null;
+
+ if (node.isValueNode() || node.isNull()) {
+ type = ODataPropertyType.PRIMITIVE;
+
+ EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.String;
+ if (node.isShort()) {
+ kind = EdmPrimitiveTypeKind.Int16;
+ } else if (node.isInt()) {
+ kind = EdmPrimitiveTypeKind.Int32;
+ } else if (node.isLong()) {
+ kind = EdmPrimitiveTypeKind.Int64;
+ } else if (node.isBoolean()) {
+ kind = EdmPrimitiveTypeKind.Boolean;
+ } else if (node.isFloat()) {
+ kind = EdmPrimitiveTypeKind.Single;
+ } else if (node.isDouble()) {
+ kind = EdmPrimitiveTypeKind.Double;
+ } else if (node.isBigDecimal()) {
+ kind = EdmPrimitiveTypeKind.Decimal;
+ }
+ typeInfo = new EdmTypeInfo.Builder().setTypeExpression(kind.getFullQualifiedName().toString()).build();
+ } else if (node.isArray()) {
+ type = ODataPropertyType.COLLECTION;
+ } else if (node.isObject()) {
+ if (node.has(Constants.ATTR_TYPE)) {
+ type = ODataPropertyType.PRIMITIVE;
+ typeInfo = new EdmTypeInfo.Builder().
+ setTypeExpression("Edm.Geography" + node.get(Constants.ATTR_TYPE).asText()).build();
+ } else {
+ type = ODataPropertyType.COMPLEX;
+ }
+ } else {
+ type = ODataPropertyType.EMPTY;
+ }
+
+ return new SimpleEntry<ODataPropertyType, EdmTypeInfo>(type, typeInfo);
+ }
+
+ protected void populate(final Annotatable annotatable, final List<Property> properties,
+ final ObjectNode tree, final ObjectCodec codec) throws IOException {
+
+ String type = null;
+ Annotation annotation = null;
+ for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
+ final Map.Entry<String, JsonNode> field = itor.next();
+ final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());
+
+ if (field.getKey().charAt(0) == '@') {
+ final Annotation entityAnnot = new AnnotationImpl();
+ entityAnnot.setTerm(field.getKey().substring(1));
+
+ value(entityAnnot, field.getValue(), codec);
+ if (annotatable != null) {
+ annotatable.getAnnotations().add(entityAnnot);
+ }
+ } else if (type == null && field.getKey().endsWith(getJSONAnnotation(jsonType))) {
+ type = field.getValue().asText();
+ } else if (annotation == null && customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
+ annotation = new AnnotationImpl();
+ annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
+ value(annotation, field.getValue(), codec);
+ } else {
+ final PropertyImpl property = new PropertyImpl();
+ property.setName(field.getKey());
+ property.setType(type == null
+ ? null
+ : new EdmTypeInfo.Builder().setTypeExpression(type).build().internal());
+ type = null;
+
+ value(property, field.getValue(), codec);
+ properties.add(property);
+
+ if (annotation != null) {
+ property.getAnnotations().add(annotation);
+ annotation = null;
+ }
+ }
+ }
+ }
+
+ private Value fromPrimitive(final JsonNode node, final EdmTypeInfo typeInfo) {
+ final Value value;
+
+ if (node.isNull()) {
+ value = new NullValueImpl();
+ } else {
+ if (typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) {
+ value = new GeospatialValueImpl(getGeoDeserializer().deserialize(node, typeInfo));
+ } else {
+ value = new PrimitiveValueImpl(node.asText());
+ }
+ }
+
+ return value;
+ }
+
+ private ComplexValue fromComplex(final ObjectNode node, final ObjectCodec codec) throws IOException {
+ final ComplexValue value = version.compareTo(ODataServiceVersion.V40) < 0
+ ? new ComplexValueImpl()
+ : new LinkedComplexValueImpl();
+
+ if (value.isLinkedComplex()) {
+ final Set<String> toRemove = new HashSet<String>();
+ for (final Iterator<Map.Entry<String, JsonNode>> itor = node.fields(); itor.hasNext();) {
+ final Map.Entry<String, JsonNode> field = itor.next();
+
+ links(field, value.asLinkedComplex(), toRemove, node, codec);
+ }
+ node.remove(toRemove);
+ }
+
+ populate(value.asLinkedComplex(), value.get(), node, codec);
+
+ return value;
+ }
+
+ private CollectionValue fromCollection(final Iterator<JsonNode> nodeItor, final EdmTypeInfo typeInfo,
+ final ObjectCodec codec) throws IOException {
+
+ final CollectionValueImpl value = new CollectionValueImpl();
+
+ final EdmTypeInfo type = typeInfo == null
+ ? null
+ : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();
+
+ while (nodeItor.hasNext()) {
+ final JsonNode child = nodeItor.next();
+
+ if (child.isValueNode()) {
+ if (typeInfo == null || typeInfo.isPrimitiveType()) {
+ value.get().add(fromPrimitive(child, type));
+ } else {
+ value.get().add(new EnumValueImpl(child.asText()));
+ }
+ } else if (child.isContainerNode()) {
+ if (child.has(jsonType)) {
+ ((ObjectNode) child).remove(jsonType);
+ }
+ value.get().add(fromComplex((ObjectNode) child, codec));
+ }
+ }
+
+ return value;
+ }
+
+ protected void value(final Valuable valuable, final JsonNode node, final ObjectCodec codec)
+ throws IOException {
+
+ EdmTypeInfo typeInfo = StringUtils.isBlank(valuable.getType())
+ ? null
+ : new EdmTypeInfo.Builder().setTypeExpression(valuable.getType()).build();
+
+ final Map.Entry<ODataPropertyType, EdmTypeInfo> guessed = guessPropertyType(node);
+ if (typeInfo == null) {
+ typeInfo = guessed.getValue();
+ }
+
+ final ODataPropertyType propType = typeInfo == null
+ ? guessed.getKey()
+ : typeInfo.isCollection()
+ ? ODataPropertyType.COLLECTION
+ : typeInfo.isPrimitiveType()
+ ? ODataPropertyType.PRIMITIVE
+ : node.isValueNode()
+ ? ODataPropertyType.ENUM
+ : ODataPropertyType.COMPLEX;
+
+ switch (propType) {
+ case COLLECTION:
+ valuable.setValue(fromCollection(node.elements(), typeInfo, codec));
+ break;
+
+ case COMPLEX:
+ if (node.has(jsonType)) {
+ valuable.setType(node.get(jsonType).asText());
+ ((ObjectNode) node).remove(jsonType);
+ }
+ valuable.setValue(fromComplex((ObjectNode) node, codec));
+ break;
+
+ case ENUM:
+ valuable.setValue(new EnumValueImpl(node.asText()));
+ break;
+
+ case PRIMITIVE:
+ if (valuable.getType() == null && typeInfo != null) {
+ valuable.setType(typeInfo.getFullQualifiedName().toString());
+ }
+ valuable.setValue(fromPrimitive(node, typeInfo));
+ break;
+
+ case EMPTY:
+ default:
+ valuable.setValue(new PrimitiveValueImpl(StringUtils.EMPTY));
+ }
+ }
+
+ @Override
+ public ResWrap<EntitySet> toEntitySet(InputStream input) throws ODataDeserializerException {
+ try {
+ parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return new JsonEntitySetDeserializer(version, serverMode).doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ @Override
+ public ResWrap<Entity> toEntity(InputStream input) throws ODataDeserializerException {
+ try {
+ parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return new JsonEntityDeserializer(version, serverMode).doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ @Override
+ public ResWrap<Property> toProperty(InputStream input) throws ODataDeserializerException {
+ try {
+ parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return new JsonPropertyDeserializer(version, serverMode).doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ @Override
+ public ODataError toError(InputStream input) throws ODataDeserializerException {
+ try {
+ parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return new JsonODataErrorDeserializer(version, serverMode).doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntityDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntityDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntityDeserializer.java
new file mode 100644
index 0000000..af5d42f
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntityDeserializer.java
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.domain.ODataLinkType;
+import org.apache.olingo.commons.api.domain.ODataOperation;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.data.AnnotationImpl;
+import org.apache.olingo.commons.core.data.EntityImpl;
+import org.apache.olingo.commons.core.data.LinkImpl;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Reads JSON string into an entity.
+ * <br/>
+ * If metadata information is available, the corresponding entity fields and content will be populated.
+ */
+public class JsonEntityDeserializer extends JsonDeserializer {
+
+ public JsonEntityDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<Entity> doDeserialize(final JsonParser parser) throws IOException {
+
+ final ObjectNode tree = parser.getCodec().readTree(parser);
+
+ if (tree.has(Constants.VALUE) && tree.get(Constants.VALUE).isArray()) {
+ throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
+ }
+
+ final EntityImpl entity = new EntityImpl();
+
+ final URI contextURL;
+ if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
+ contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
+ tree.remove(Constants.JSON_CONTEXT);
+ } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
+ contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
+ tree.remove(Constants.JSON_METADATA);
+ } else {
+ contextURL = null;
+ }
+ if (contextURL != null) {
+ entity.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
+ }
+
+ final String metadataETag;
+ if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
+ metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
+ tree.remove(Constants.JSON_METADATA_ETAG);
+ } else {
+ metadataETag = null;
+ }
+
+ if (tree.hasNonNull(jsonETag)) {
+ entity.setETag(tree.get(jsonETag).textValue());
+ tree.remove(jsonETag);
+ }
+
+ if (tree.hasNonNull(jsonType)) {
+ entity.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
+ tree.remove(jsonType);
+ }
+
+ if (tree.hasNonNull(jsonId)) {
+ entity.setId(URI.create(tree.get(jsonId).textValue()));
+ tree.remove(jsonId);
+ }
+
+ if (tree.hasNonNull(jsonReadLink)) {
+ final LinkImpl link = new LinkImpl();
+ link.setRel(Constants.SELF_LINK_REL);
+ link.setHref(tree.get(jsonReadLink).textValue());
+ entity.setSelfLink(link);
+
+ tree.remove(jsonReadLink);
+ }
+
+ if (tree.hasNonNull(jsonEditLink)) {
+ final LinkImpl link = new LinkImpl();
+ if (serverMode) {
+ link.setRel(Constants.EDIT_LINK_REL);
+ }
+ link.setHref(tree.get(jsonEditLink).textValue());
+ entity.setEditLink(link);
+
+ tree.remove(jsonEditLink);
+ }
+
+ if (tree.hasNonNull(jsonMediaReadLink)) {
+ entity.setMediaContentSource(URI.create(tree.get(jsonMediaReadLink).textValue()));
+ tree.remove(jsonMediaReadLink);
+ }
+ if (tree.hasNonNull(jsonMediaEditLink)) {
+ entity.setMediaContentSource(URI.create(tree.get(jsonMediaEditLink).textValue()));
+ tree.remove(jsonMediaEditLink);
+ }
+ if (tree.hasNonNull(jsonMediaContentType)) {
+ entity.setMediaContentType(tree.get(jsonMediaContentType).textValue());
+ tree.remove(jsonMediaContentType);
+ }
+ if (tree.hasNonNull(jsonMediaETag)) {
+ entity.setMediaETag(tree.get(jsonMediaETag).textValue());
+ tree.remove(jsonMediaETag);
+ }
+
+ final Set<String> toRemove = new HashSet<String>();
+
+ final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
+ for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
+ final Map.Entry<String, JsonNode> field = itor.next();
+ final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());
+
+ links(field, entity, toRemove, tree, parser.getCodec());
+ if (field.getKey().endsWith(getJSONAnnotation(jsonMediaEditLink))) {
+ final LinkImpl link = new LinkImpl();
+ link.setTitle(getTitle(field));
+ link.setRel(version.getNamespaceMap().get(ODataServiceVersion.MEDIA_EDIT_LINK_REL) + getTitle(field));
+ link.setHref(field.getValue().textValue());
+ link.setType(ODataLinkType.MEDIA_EDIT.toString());
+ entity.getMediaEditLinks().add(link);
+
+ if (tree.has(link.getTitle() + getJSONAnnotation(jsonMediaETag))) {
+ link.setMediaETag(tree.get(link.getTitle() + getJSONAnnotation(jsonMediaETag)).asText());
+ toRemove.add(link.getTitle() + getJSONAnnotation(jsonMediaETag));
+ }
+
+ toRemove.add(field.getKey());
+ toRemove.add(setInline(field.getKey(), getJSONAnnotation(jsonMediaEditLink), tree, parser.getCodec(), link));
+ } else if (field.getKey().endsWith(getJSONAnnotation(jsonMediaContentType))) {
+ final String linkTitle = getTitle(field);
+ for (Link link : entity.getMediaEditLinks()) {
+ if (linkTitle.equals(link.getTitle())) {
+ ((LinkImpl) link).setType(field.getValue().asText());
+ }
+ }
+ toRemove.add(field.getKey());
+ } else if (field.getKey().charAt(0) == '#') {
+ final ODataOperation operation = new ODataOperation();
+ operation.setMetadataAnchor(field.getKey());
+
+ final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
+ operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText());
+ operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText()));
+
+ entity.getOperations().add(operation);
+
+ toRemove.add(field.getKey());
+ } else if (customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
+ final Annotation annotation = new AnnotationImpl();
+ annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
+ value(annotation, field.getValue(), parser.getCodec());
+
+ if (!annotations.containsKey(customAnnotation.group(1))) {
+ annotations.put(customAnnotation.group(1), new ArrayList<Annotation>());
+ }
+ annotations.get(customAnnotation.group(1)).add(annotation);
+ }
+ }
+
+ for (Link link : entity.getNavigationLinks()) {
+ if (annotations.containsKey(link.getTitle())) {
+ link.getAnnotations().addAll(annotations.get(link.getTitle()));
+ for (Annotation annotation : annotations.get(link.getTitle())) {
+ toRemove.add(link.getTitle() + "@" + annotation.getTerm());
+ }
+ }
+ }
+ for (Link link : entity.getMediaEditLinks()) {
+ if (annotations.containsKey(link.getTitle())) {
+ link.getAnnotations().addAll(annotations.get(link.getTitle()));
+ for (Annotation annotation : annotations.get(link.getTitle())) {
+ toRemove.add(link.getTitle() + "@" + annotation.getTerm());
+ }
+ }
+ }
+
+ tree.remove(toRemove);
+
+ populate(entity, entity.getProperties(), tree, parser.getCodec());
+
+ return new ResWrap<Entity>(contextURL, metadataETag, entity);
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java
new file mode 100644
index 0000000..22d6a92
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.domain.ODataOperation;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+
+/**
+ * Writes out JSON string from an entity.
+ */
+public class JsonEntitySerializer extends JsonSerializer {
+
+ public JsonEntitySerializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected void doSerialize(final Entity entity, final JsonGenerator jgen) throws IOException {
+ doContainerSerialize(new ResWrap<Entity>((URI) null, null, entity), jgen);
+ }
+
+ protected void doContainerSerialize(final ResWrap<Entity> container, final JsonGenerator jgen)
+ throws IOException {
+
+ final Entity entity = container.getPayload();
+
+ jgen.writeStartObject();
+
+ if (serverMode) {
+ if (container.getContextURL() != null) {
+ jgen.writeStringField(version.compareTo(ODataServiceVersion.V40) >= 0
+ ? Constants.JSON_CONTEXT : Constants.JSON_METADATA,
+ container.getContextURL().getURI().toASCIIString());
+ }
+ if (version.compareTo(ODataServiceVersion.V40) >= 0 && StringUtils.isNotBlank(container.getMetadataETag())) {
+ jgen.writeStringField(Constants.JSON_METADATA_ETAG, container.getMetadataETag());
+ }
+
+ if (StringUtils.isNotBlank(entity.getETag())) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_ETAG), entity.getETag());
+ }
+ }
+
+ if (StringUtils.isNotBlank(entity.getType())) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE),
+ new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version));
+ }
+
+ if (entity.getId() != null) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_ID), entity.getId().toASCIIString());
+ }
+
+ for (Annotation annotation : entity.getAnnotations()) {
+ valuable(jgen, annotation, "@" + annotation.getTerm());
+ }
+
+ for (Property property : entity.getProperties()) {
+ valuable(jgen, property, property.getName());
+ }
+
+ if (serverMode && entity.getEditLink() != null && StringUtils.isNotBlank(entity.getEditLink().getHref())) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_EDIT_LINK),
+ entity.getEditLink().getHref());
+
+ if (entity.isMediaEntity()) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAREAD_LINK),
+ entity.getEditLink().getHref() + "/$value");
+ }
+ }
+
+ links(entity, jgen);
+
+ for (Link link : entity.getMediaEditLinks()) {
+ if (link.getTitle() == null) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAEDIT_LINK), link.getHref());
+ }
+
+ if (link.getInlineEntity() != null) {
+ jgen.writeObjectField(link.getTitle(), link.getInlineEntity());
+ }
+ if (link.getInlineEntitySet() != null) {
+ jgen.writeArrayFieldStart(link.getTitle());
+ for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
+ jgen.writeObject(subEntry);
+ }
+ jgen.writeEndArray();
+ }
+ }
+
+ if (serverMode) {
+ for (ODataOperation operation : entity.getOperations()) {
+ jgen.writeObjectFieldStart("#" + StringUtils.substringAfterLast(operation.getMetadataAnchor(), "#"));
+ jgen.writeStringField(Constants.ATTR_TITLE, operation.getTitle());
+ jgen.writeStringField(Constants.ATTR_TARGET, operation.getTarget().toASCIIString());
+ jgen.writeEndObject();
+ }
+ }
+
+ jgen.writeEndObject();
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetDeserializer.java
new file mode 100644
index 0000000..d7f86fb
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetDeserializer.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.EntitySet;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.data.AnnotationImpl;
+import org.apache.olingo.commons.core.data.EntitySetImpl;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Reads JSON string into an entity set.
+ * <br/>
+ * If metadata information is available, the corresponding entity fields and content will be populated.
+ */
+public class JsonEntitySetDeserializer extends JsonDeserializer {
+
+ public JsonEntitySetDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<EntitySet> doDeserialize(final JsonParser parser) throws IOException {
+
+ final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);
+
+ if (!tree.has(Constants.VALUE)) {
+ return null;
+ }
+
+ final EntitySetImpl entitySet = new EntitySetImpl();
+
+ URI contextURL;
+ if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
+ contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
+ tree.remove(Constants.JSON_CONTEXT);
+ } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
+ contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
+ tree.remove(Constants.JSON_METADATA);
+ } else {
+ contextURL = null;
+ }
+ if (contextURL != null) {
+ entitySet.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
+ }
+
+ final String metadataETag;
+ if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
+ metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
+ tree.remove(Constants.JSON_METADATA_ETAG);
+ } else {
+ metadataETag = null;
+ }
+
+ if (tree.hasNonNull(jsonCount)) {
+ entitySet.setCount(tree.get(jsonCount).asInt());
+ tree.remove(jsonCount);
+ }
+ if (tree.hasNonNull(jsonNextLink)) {
+ entitySet.setNext(URI.create(tree.get(jsonNextLink).textValue()));
+ tree.remove(jsonNextLink);
+ }
+ if (tree.hasNonNull(jsonDeltaLink)) {
+ entitySet.setDeltaLink(URI.create(tree.get(jsonDeltaLink).textValue()));
+ tree.remove(jsonDeltaLink);
+ }
+
+ if (tree.hasNonNull(Constants.VALUE)) {
+ final JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(version, serverMode);
+ for (final Iterator<JsonNode> itor = tree.get(Constants.VALUE).iterator(); itor.hasNext();) {
+ entitySet.getEntities().add(
+ entityDeserializer.doDeserialize(itor.next().traverse(parser.getCodec())).getPayload());
+ }
+ tree.remove(Constants.VALUE);
+ }
+
+ // any remaining entry is supposed to be an annotation or is ignored
+ for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
+ final Map.Entry<String, JsonNode> field = itor.next();
+ if (field.getKey().charAt(0) == '@') {
+ final Annotation annotation = new AnnotationImpl();
+ annotation.setTerm(field.getKey().substring(1));
+
+ value(annotation, field.getValue(), parser.getCodec());
+ entitySet.getAnnotations().add(annotation);
+ }
+ }
+
+ return new ResWrap<EntitySet>(contextURL, metadataETag, entitySet);
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java
new file mode 100644
index 0000000..e1cc38b
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntitySet;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+
+public class JsonEntitySetSerializer extends JsonSerializer {
+
+ public JsonEntitySetSerializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected void doSerialize(final EntitySet entitySet, final JsonGenerator jgen) throws IOException {
+ doContainerSerialize(new ResWrap<EntitySet>((URI) null, null, entitySet), jgen);
+ }
+
+ protected void doContainerSerialize(final ResWrap<EntitySet> container, final JsonGenerator jgen)
+ throws IOException {
+
+ final EntitySet entitySet = container.getPayload();
+
+ jgen.writeStartObject();
+
+ if (serverMode) {
+ if (container.getContextURL() != null) {
+ jgen.writeStringField(version.compareTo(ODataServiceVersion.V40) >= 0
+ ? Constants.JSON_CONTEXT : Constants.JSON_METADATA,
+ container.getContextURL().getURI().toASCIIString());
+ }
+
+ if (version.compareTo(ODataServiceVersion.V40) >= 0 && StringUtils.isNotBlank(container.getMetadataETag())) {
+ jgen.writeStringField(
+ Constants.JSON_METADATA_ETAG,
+ container.getMetadataETag());
+ }
+ }
+
+ if (entitySet.getId() != null) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_ID), entitySet.getId().toASCIIString());
+ }
+ jgen.writeNumberField(version.getJSONMap().get(ODataServiceVersion.JSON_COUNT),
+ entitySet.getCount() == null ? entitySet.getEntities().size() : entitySet.getCount());
+ if (serverMode) {
+ if (entitySet.getNext() != null) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_NEXT_LINK),
+ entitySet.getNext().toASCIIString());
+ }
+ if (entitySet.getDeltaLink() != null) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_DELTA_LINK),
+ entitySet.getDeltaLink().toASCIIString());
+ }
+ }
+
+ for (Annotation annotation : entitySet.getAnnotations()) {
+ valuable(jgen, annotation, "@" + annotation.getTerm());
+ }
+
+ jgen.writeArrayFieldStart(Constants.VALUE);
+ final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(version, serverMode);
+ for (Entity entity : entitySet.getEntities()) {
+ entitySerializer.doSerialize(entity, jgen);
+ }
+ jgen.writeEndArray();
+
+ jgen.writeEndObject();
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueDeserializer.java
new file mode 100644
index 0000000..8d2db37
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueDeserializer.java
@@ -0,0 +1,267 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.GeoUtils;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.edm.geo.Geospatial;
+import org.apache.olingo.commons.api.edm.geo.GeospatialCollection;
+import org.apache.olingo.commons.api.edm.geo.LineString;
+import org.apache.olingo.commons.api.edm.geo.MultiLineString;
+import org.apache.olingo.commons.api.edm.geo.MultiPoint;
+import org.apache.olingo.commons.api.edm.geo.MultiPolygon;
+import org.apache.olingo.commons.api.edm.geo.Point;
+import org.apache.olingo.commons.api.edm.geo.Polygon;
+import org.apache.olingo.commons.api.edm.geo.SRID;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+import org.apache.olingo.commons.core.edm.primitivetype.EdmDouble;
+
+class JsonGeoValueDeserializer {
+
+ private final ODataServiceVersion version;
+
+ public JsonGeoValueDeserializer(final ODataServiceVersion version) {
+ this.version = version;
+ }
+
+ private Point point(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type, final SRID srid) {
+ Point point = null;
+
+ if (itor.hasNext()) {
+ point = new Point(GeoUtils.getDimension(type), srid);
+ try {
+ point.setX(EdmDouble.getInstance().valueOfString(itor.next().asText(), null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, Double.class));
+ point.setY(EdmDouble.getInstance().valueOfString(itor.next().asText(), null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, Double.class));
+ } catch (EdmPrimitiveTypeException e) {
+ throw new IllegalArgumentException("While deserializing point coordinates as double", e);
+ }
+ }
+
+ return point;
+ }
+
+ private MultiPoint multipoint(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type, final SRID srid) {
+ final MultiPoint multiPoint;
+
+ if (itor.hasNext()) {
+ final List<Point> points = new ArrayList<Point>();
+ while (itor.hasNext()) {
+ final Iterator<JsonNode> mpItor = itor.next().elements();
+ points.add(point(mpItor, type, srid));
+ }
+ multiPoint = new MultiPoint(GeoUtils.getDimension(type), srid, points);
+ } else {
+ multiPoint = new MultiPoint(GeoUtils.getDimension(type), srid, Collections.<Point>emptyList());
+ }
+
+ return multiPoint;
+ }
+
+ private LineString lineString(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type, final SRID srid) {
+ final LineString lineString;
+
+ if (itor.hasNext()) {
+ final List<Point> points = new ArrayList<Point>();
+ while (itor.hasNext()) {
+ final Iterator<JsonNode> mpItor = itor.next().elements();
+ points.add(point(mpItor, type, srid));
+ }
+ lineString = new LineString(GeoUtils.getDimension(type), srid, points);
+ } else {
+ lineString = new LineString(GeoUtils.getDimension(type), srid, Collections.<Point>emptyList());
+ }
+
+ return lineString;
+ }
+
+ private MultiLineString multiLineString(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type,
+ final SRID srid) {
+
+ final MultiLineString multiLineString;
+
+ if (itor.hasNext()) {
+ final List<LineString> lineStrings = new ArrayList<LineString>();
+ while (itor.hasNext()) {
+ final Iterator<JsonNode> mlsItor = itor.next().elements();
+ lineStrings.add(lineString(mlsItor, type, srid));
+ }
+ multiLineString = new MultiLineString(GeoUtils.getDimension(type), srid, lineStrings);
+ } else {
+ multiLineString = new MultiLineString(GeoUtils.getDimension(type), srid, Collections.<LineString>emptyList());
+ }
+
+ return multiLineString;
+ }
+
+ private Polygon polygon(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type, final SRID srid) {
+ List<Point> extPoints = null;
+ if (itor.hasNext()) {
+ final Iterator<JsonNode> extItor = itor.next().elements();
+ if (extItor.hasNext()) {
+ extPoints = new ArrayList<Point>();
+ while (extItor.hasNext()) {
+ final Iterator<JsonNode> mpItor = extItor.next().elements();
+ extPoints.add(point(mpItor, type, srid));
+ }
+ }
+ }
+
+ List<Point> intPoints = null;
+ if (itor.hasNext()) {
+ final Iterator<JsonNode> intItor = itor.next().elements();
+ if (intItor.hasNext()) {
+ intPoints = new ArrayList<Point>();
+ while (intItor.hasNext()) {
+ final Iterator<JsonNode> mpItor = intItor.next().elements();
+ intPoints.add(point(mpItor, type, srid));
+ }
+ }
+ }
+
+ return new Polygon(GeoUtils.getDimension(type), srid, intPoints, extPoints);
+ }
+
+ private MultiPolygon multiPolygon(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type, final SRID srid) {
+ final MultiPolygon multiPolygon;
+
+ if (itor.hasNext()) {
+ final List<Polygon> polygons = new ArrayList<Polygon>();
+ while (itor.hasNext()) {
+ final Iterator<JsonNode> mpItor = itor.next().elements();
+ polygons.add(polygon(mpItor, type, srid));
+ }
+ multiPolygon = new MultiPolygon(GeoUtils.getDimension(type), srid, polygons);
+ } else {
+ multiPolygon = new MultiPolygon(GeoUtils.getDimension(type), srid, Collections.<Polygon>emptyList());
+ }
+
+ return multiPolygon;
+ }
+
+ private GeospatialCollection collection(final Iterator<JsonNode> itor, final EdmPrimitiveTypeKind type,
+ final SRID srid) {
+
+ final GeospatialCollection collection;
+
+ if (itor.hasNext()) {
+ final List<Geospatial> geospatials = new ArrayList<Geospatial>();
+
+ while (itor.hasNext()) {
+ final JsonNode geo = itor.next();
+ final String collItemType = geo.get(Constants.ATTR_TYPE).asText();
+ final String callAsType;
+ if (EdmPrimitiveTypeKind.GeographyCollection.name().equals(collItemType)
+ || EdmPrimitiveTypeKind.GeometryCollection.name().equals(collItemType)) {
+
+ callAsType = collItemType;
+ } else {
+ callAsType = (type == EdmPrimitiveTypeKind.GeographyCollection ? "Geography" : "Geometry")
+ + collItemType;
+ }
+
+ geospatials.add(deserialize(geo, new EdmTypeInfo.Builder().setTypeExpression(callAsType).build()));
+ }
+
+ collection = new GeospatialCollection(GeoUtils.getDimension(type), srid, geospatials);
+ } else {
+ collection = new GeospatialCollection(GeoUtils.getDimension(type), srid, Collections.<Geospatial>emptyList());
+ }
+
+ return collection;
+ }
+
+ public Geospatial deserialize(final JsonNode node, final EdmTypeInfo typeInfo) {
+ final EdmPrimitiveTypeKind actualType;
+ if ((typeInfo.getPrimitiveTypeKind() == EdmPrimitiveTypeKind.Geography
+ || typeInfo.getPrimitiveTypeKind() == EdmPrimitiveTypeKind.Geometry)
+ && node.has(Constants.ATTR_TYPE)) {
+
+ String nodeType = node.get(Constants.ATTR_TYPE).asText();
+ if (nodeType.startsWith("Geo")) {
+ final int yIdx = nodeType.indexOf('y');
+ nodeType = nodeType.substring(yIdx + 1);
+ }
+ actualType = EdmPrimitiveTypeKind.valueOfFQN(version, typeInfo.getFullQualifiedName().toString() + nodeType);
+ } else {
+ actualType = typeInfo.getPrimitiveTypeKind();
+ }
+
+ final Iterator<JsonNode> cooItor = node.has(Constants.JSON_COORDINATES)
+ ? node.get(Constants.JSON_COORDINATES).elements()
+ : Collections.<JsonNode>emptyList().iterator();
+
+ SRID srid = null;
+ if (node.has(Constants.JSON_CRS)) {
+ srid = SRID.valueOf(
+ node.get(Constants.JSON_CRS).get(Constants.PROPERTIES).get(Constants.JSON_NAME).asText().split(":")[1]);
+ }
+
+ Geospatial value = null;
+ switch (actualType) {
+ case GeographyPoint:
+ case GeometryPoint:
+ value = point(cooItor, actualType, srid);
+ break;
+
+ case GeographyMultiPoint:
+ case GeometryMultiPoint:
+ value = multipoint(cooItor, actualType, srid);
+ break;
+
+ case GeographyLineString:
+ case GeometryLineString:
+ value = lineString(cooItor, actualType, srid);
+ break;
+
+ case GeographyMultiLineString:
+ case GeometryMultiLineString:
+ value = multiLineString(cooItor, actualType, srid);
+ break;
+
+ case GeographyPolygon:
+ case GeometryPolygon:
+ value = polygon(cooItor, actualType, srid);
+ break;
+
+ case GeographyMultiPolygon:
+ case GeometryMultiPolygon:
+ value = multiPolygon(cooItor, actualType, srid);
+ break;
+
+ case GeographyCollection:
+ case GeometryCollection:
+ value = collection(node.get(Constants.JSON_GEOMETRIES).elements(), actualType, srid);
+ break;
+
+ default:
+ }
+
+ return value;
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueSerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueSerializer.java
new file mode 100644
index 0000000..cfc026c
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonGeoValueSerializer.java
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import java.io.IOException;
+import java.util.Iterator;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.geo.ComposedGeospatial;
+import org.apache.olingo.commons.api.edm.geo.Geospatial;
+import org.apache.olingo.commons.api.edm.geo.GeospatialCollection;
+import org.apache.olingo.commons.api.edm.geo.LineString;
+import org.apache.olingo.commons.api.edm.geo.MultiLineString;
+import org.apache.olingo.commons.api.edm.geo.MultiPoint;
+import org.apache.olingo.commons.api.edm.geo.MultiPolygon;
+import org.apache.olingo.commons.api.edm.geo.Point;
+import org.apache.olingo.commons.api.edm.geo.Polygon;
+import org.apache.olingo.commons.api.edm.geo.SRID;
+import org.apache.olingo.commons.core.edm.primitivetype.EdmDouble;
+
+class JsonGeoValueSerializer {
+
+ private void srid(final JsonGenerator jgen, final SRID srid) throws IOException {
+ jgen.writeObjectFieldStart(Constants.JSON_CRS);
+ jgen.writeStringField(Constants.ATTR_TYPE, Constants.JSON_NAME);
+ jgen.writeObjectFieldStart(Constants.PROPERTIES);
+ jgen.writeStringField(Constants.JSON_NAME, "EPSG:" + srid.toString());
+ jgen.writeEndObject();
+ jgen.writeEndObject();
+ }
+
+ private void point(final JsonGenerator jgen, final Point point) throws IOException {
+ try {
+ jgen.writeNumber(EdmDouble.getInstance().valueToString(point.getX(), null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null));
+ jgen.writeNumber(EdmDouble.getInstance().valueToString(point.getY(), null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null));
+ } catch (EdmPrimitiveTypeException e) {
+ throw new IllegalArgumentException("While serializing point coordinates as double", e);
+ }
+ }
+
+ private void multipoint(final JsonGenerator jgen, final MultiPoint multiPoint) throws IOException {
+ for (final Iterator<Point> itor = multiPoint.iterator(); itor.hasNext();) {
+ jgen.writeStartArray();
+ point(jgen, itor.next());
+ jgen.writeEndArray();
+ }
+ }
+
+ private void lineString(final JsonGenerator jgen, final ComposedGeospatial<Point> lineString) throws IOException {
+ for (final Iterator<Point> itor = lineString.iterator(); itor.hasNext();) {
+ jgen.writeStartArray();
+ point(jgen, itor.next());
+ jgen.writeEndArray();
+ }
+ }
+
+ private void multiLineString(final JsonGenerator jgen, final MultiLineString multiLineString) throws IOException {
+ for (final Iterator<LineString> itor = multiLineString.iterator(); itor.hasNext();) {
+ jgen.writeStartArray();
+ lineString(jgen, itor.next());
+ jgen.writeEndArray();
+ }
+ }
+
+ private void polygon(final JsonGenerator jgen, final Polygon polygon) throws IOException {
+ if (!polygon.getExterior().isEmpty()) {
+ jgen.writeStartArray();
+ lineString(jgen, polygon.getExterior());
+ jgen.writeEndArray();
+ }
+ if (!polygon.getInterior().isEmpty()) {
+ jgen.writeStartArray();
+ lineString(jgen, polygon.getInterior());
+ jgen.writeEndArray();
+ }
+ }
+
+ private void multiPolygon(final JsonGenerator jgen, final MultiPolygon multiPolygon) throws IOException {
+ for (final Iterator<Polygon> itor = multiPolygon.iterator(); itor.hasNext();) {
+ final Polygon polygon = itor.next();
+ jgen.writeStartArray();
+ polygon(jgen, polygon);
+ jgen.writeEndArray();
+ }
+ }
+
+ private void collection(final JsonGenerator jgen, final GeospatialCollection collection) throws IOException {
+ jgen.writeArrayFieldStart(Constants.JSON_GEOMETRIES);
+ for (final Iterator<Geospatial> itor = collection.iterator(); itor.hasNext();) {
+ jgen.writeStartObject();
+ serialize(jgen, itor.next());
+ jgen.writeEndObject();
+ }
+ jgen.writeEndArray();
+ }
+
+ public void serialize(final JsonGenerator jgen, final Geospatial value) throws IOException {
+ if (value.getEdmPrimitiveTypeKind().equals(EdmPrimitiveTypeKind.GeographyCollection)
+ || value.getEdmPrimitiveTypeKind().equals(EdmPrimitiveTypeKind.GeometryCollection)) {
+
+ jgen.writeStringField(Constants.ATTR_TYPE, EdmPrimitiveTypeKind.GeometryCollection.name());
+ } else {
+ final int yIdx = value.getEdmPrimitiveTypeKind().name().indexOf('y');
+ final String itemType = value.getEdmPrimitiveTypeKind().name().substring(yIdx + 1);
+ jgen.writeStringField(Constants.ATTR_TYPE, itemType);
+ }
+
+ switch (value.getEdmPrimitiveTypeKind()) {
+ case GeographyPoint:
+ case GeometryPoint:
+ jgen.writeArrayFieldStart(Constants.JSON_COORDINATES);
+ point(jgen, (Point) value);
+ jgen.writeEndArray();
+ break;
+
+ case GeographyMultiPoint:
+ case GeometryMultiPoint:
+ jgen.writeArrayFieldStart(Constants.JSON_COORDINATES);
+ multipoint(jgen, (MultiPoint) value);
+ jgen.writeEndArray();
+ break;
+
+ case GeographyLineString:
+ case GeometryLineString:
+ jgen.writeArrayFieldStart(Constants.JSON_COORDINATES);
+ lineString(jgen, (LineString) value);
+ jgen.writeEndArray();
+ break;
+
+ case GeographyMultiLineString:
+ case GeometryMultiLineString:
+ jgen.writeArrayFieldStart(Constants.JSON_COORDINATES);
+ multiLineString(jgen, (MultiLineString) value);
+ jgen.writeEndArray();
+ break;
+
+ case GeographyPolygon:
+ case GeometryPolygon:
+ jgen.writeArrayFieldStart(Constants.JSON_COORDINATES);
+ polygon(jgen, (Polygon) value);
+ jgen.writeEndArray();
+ break;
+
+ case GeographyMultiPolygon:
+ case GeometryMultiPolygon:
+ jgen.writeArrayFieldStart(Constants.JSON_COORDINATES);
+ multiPolygon(jgen, (MultiPolygon) value);
+ jgen.writeEndArray();
+ break;
+
+ case GeographyCollection:
+ case GeometryCollection:
+ collection(jgen, (GeospatialCollection) value);
+ break;
+
+ default:
+ }
+
+ if (value.getSrid() != null && value.getSrid().isNotDefault()) {
+ srid(jgen, value.getSrid());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonLinkCollectionDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonLinkCollectionDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonLinkCollectionDeserializer.java
new file mode 100755
index 0000000..feeeb43
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonLinkCollectionDeserializer.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.data.v3.LinkCollection;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.serialization.ODataDeserializerException;
+import org.apache.olingo.commons.core.data.v3.LinkCollectionImpl;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class JsonLinkCollectionDeserializer extends JsonDeserializer {
+
+ public JsonLinkCollectionDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<LinkCollection> doDeserialize(final JsonParser parser) throws IOException {
+
+ final ObjectNode tree = parser.getCodec().readTree(parser);
+
+ final LinkCollectionImpl links = new LinkCollectionImpl();
+
+ if (tree.hasNonNull("odata.metadata")) {
+ links.setMetadata(URI.create(tree.get("odata.metadata").textValue()));
+ }
+
+ if (tree.hasNonNull(Constants.JSON_URL)) {
+ links.getLinks().add(URI.create(tree.get(Constants.JSON_URL).textValue()));
+ }
+
+ if (tree.hasNonNull(Constants.VALUE)) {
+ for (final JsonNode item : tree.get(Constants.VALUE)) {
+ final URI uri = URI.create(item.get(Constants.JSON_URL).textValue());
+ links.getLinks().add(uri);
+ }
+ }
+
+ if (tree.hasNonNull(jsonNextLink)) {
+ links.setNext(URI.create(tree.get(jsonNextLink).textValue()));
+ }
+
+ return new ResWrap<LinkCollection>((URI) null, null, links);
+ }
+
+ public ResWrap<LinkCollection> toLinkCollection(InputStream input) throws ODataDeserializerException {
+ try {
+ JsonParser parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDeserializer.java
new file mode 100644
index 0000000..3d44e5a
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDeserializer.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.domain.ODataError;
+import org.apache.olingo.commons.api.domain.ODataErrorDetail;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.data.ODataErrorImpl;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class JsonODataErrorDeserializer extends JsonDeserializer {
+
+ public JsonODataErrorDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ODataError doDeserialize(final JsonParser parser) throws IOException {
+
+ final ODataErrorImpl error = new ODataErrorImpl();
+
+ final ObjectNode tree = parser.getCodec().readTree(parser);
+ if (tree.has(jsonError)) {
+ final JsonNode errorNode = tree.get(jsonError);
+
+ if (errorNode.has(Constants.ERROR_CODE)) {
+ error.setCode(errorNode.get(Constants.ERROR_CODE).textValue());
+ }
+ if (errorNode.has(Constants.ERROR_MESSAGE)) {
+ final JsonNode message = errorNode.get(Constants.ERROR_MESSAGE);
+ if (message.isValueNode()) {
+ error.setMessage(message.textValue());
+ } else if (message.isObject()) {
+ error.setMessage(message.get(Constants.VALUE).asText());
+ }
+ }
+ if (errorNode.has(Constants.ERROR_TARGET)) {
+ error.setTarget(errorNode.get(Constants.ERROR_TARGET).textValue());
+ }
+ if (errorNode.hasNonNull(Constants.ERROR_DETAILS)) {
+ List<ODataErrorDetail> details = new ArrayList<ODataErrorDetail>();
+ JsonODataErrorDetailDeserializer detailDeserializer =
+ new JsonODataErrorDetailDeserializer(version, serverMode);
+ for (Iterator<JsonNode> itor = errorNode.get(Constants.ERROR_DETAILS).iterator(); itor.hasNext();) {
+ details.add(detailDeserializer.doDeserialize(itor.next().traverse(parser.getCodec()))
+ .getPayload());
+ }
+
+ error.setDetails(details);
+ }
+ if (errorNode.hasNonNull(Constants.ERROR_INNERERROR)) {
+ final JsonNode innerError = errorNode.get(Constants.ERROR_INNERERROR);
+ for (final Iterator<String> itor = innerError.fieldNames(); itor.hasNext();) {
+ final String keyTmp = itor.next();
+ final String val = innerError.get(keyTmp).toString();
+ error.getInnerError().put(keyTmp, val);
+ }
+ }
+ }
+
+ return error;
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDetailDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDetailDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDetailDeserializer.java
new file mode 100644
index 0000000..613e687
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonODataErrorDetailDeserializer.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.domain.ODataErrorDetail;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.data.ODataErrorDetailImpl;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+
+public class JsonODataErrorDetailDeserializer extends JsonDeserializer {
+
+ public JsonODataErrorDetailDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<ODataErrorDetail> doDeserialize(final JsonParser parser) throws IOException {
+
+ final ODataErrorDetailImpl error = new ODataErrorDetailImpl();
+ final JsonNode errorNode = parser.getCodec().readTree(parser);
+ if (errorNode.has(Constants.ERROR_CODE)) {
+ error.setCode(errorNode.get(Constants.ERROR_CODE).textValue());
+ }
+ if (errorNode.has(Constants.ERROR_MESSAGE)) {
+ final JsonNode message = errorNode.get(Constants.ERROR_MESSAGE);
+ if (message.isValueNode()) {
+ error.setMessage(message.textValue());
+ } else if (message.isObject()) {
+ error.setMessage(message.get(Constants.VALUE).asText());
+ }
+ }
+ if (errorNode.has(Constants.ERROR_TARGET)) {
+ error.setTarget(errorNode.get(Constants.ERROR_TARGET).textValue());
+ }
+
+ return new ResWrap<ODataErrorDetail>((URI) null, null, error);
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertyDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertyDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertyDeserializer.java
new file mode 100644
index 0000000..380d245
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertyDeserializer.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.data.AnnotationImpl;
+import org.apache.olingo.commons.core.data.NullValueImpl;
+import org.apache.olingo.commons.core.data.PropertyImpl;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Parse JSON string into <tt>Property</tt>.
+ */
+public class JsonPropertyDeserializer extends JsonDeserializer {
+
+ public JsonPropertyDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<Property> doDeserialize(final JsonParser parser) throws IOException {
+
+ final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);
+
+ final String metadataETag;
+ final URI contextURL;
+ final PropertyImpl property = new PropertyImpl();
+
+ if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
+ metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
+ tree.remove(Constants.JSON_METADATA_ETAG);
+ } else {
+ metadataETag = null;
+ }
+
+ if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
+ contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
+ property.setName(StringUtils.substringAfterLast(contextURL.toASCIIString(), "/"));
+ tree.remove(Constants.JSON_CONTEXT);
+ } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
+ contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
+ property.setType(new EdmTypeInfo.Builder().
+ setTypeExpression(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#")).build().internal());
+ tree.remove(Constants.JSON_METADATA);
+ } else {
+ contextURL = null;
+ }
+
+ if (tree.has(jsonType)) {
+ property.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
+ tree.remove(jsonType);
+ }
+
+ if (tree.has(Constants.JSON_NULL) && tree.get(Constants.JSON_NULL).asBoolean()) {
+ property.setValue(new NullValueImpl());
+ tree.remove(Constants.JSON_NULL);
+ }
+
+ if (property.getValue() == null) {
+ value(property, tree.has(Constants.VALUE) ? tree.get(Constants.VALUE) : tree, parser.getCodec());
+ tree.remove(Constants.VALUE);
+ }
+
+ // any remaining entry is supposed to be an annotation or is ignored
+ for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
+ final Map.Entry<String, JsonNode> field = itor.next();
+ if (field.getKey().charAt(0) == '@') {
+ final Annotation annotation = new AnnotationImpl();
+ annotation.setTerm(field.getKey().substring(1));
+
+ value(annotation, field.getValue(), parser.getCodec());
+ property.getAnnotations().add(annotation);
+ }
+ }
+
+ return new ResWrap<Property>(contextURL, metadataETag, property);
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java
new file mode 100644
index 0000000..9576007
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.commons.core.serialization;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+
+/**
+ * Writes out JSON string from <tt>PropertyImpl</tt>.
+ */
+public class JsonPropertySerializer extends JsonSerializer {
+
+ public JsonPropertySerializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected void doSerialize(final Property property, final JsonGenerator jgen) throws IOException {
+ doContainerSerialize(new ResWrap<Property>((URI) null, null, property), jgen);
+ }
+
+ protected void doContainerSerialize(final ResWrap<Property> container, final JsonGenerator jgen)
+ throws IOException {
+
+ final Property property = container.getPayload();
+
+ jgen.writeStartObject();
+
+ if (serverMode && container.getContextURL() != null) {
+ jgen.writeStringField(version.compareTo(ODataServiceVersion.V40) >= 0
+ ? Constants.JSON_CONTEXT : Constants.JSON_METADATA,
+ container.getContextURL().getURI().toASCIIString());
+ }
+
+ if (StringUtils.isNotBlank(property.getType())) {
+ jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE),
+ new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build().external(version));
+ }
+
+ for (Annotation annotation : property.getAnnotations()) {
+ valuable(jgen, annotation, "@" + annotation.getTerm());
+ }
+
+ if (property.getValue().isNull()) {
+ jgen.writeBooleanField(Constants.JSON_NULL, true);
+ } else if (property.getValue().isPrimitive()) {
+ final EdmTypeInfo typeInfo = property.getType() == null
+ ? null
+ : new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
+
+ jgen.writeFieldName(Constants.VALUE);
+ primitiveValue(jgen, typeInfo, property.getValue().asPrimitive());
+ } else if (property.getValue().isEnum()) {
+ jgen.writeStringField(Constants.VALUE, property.getValue().asEnum().get());
+ } else if (property.getValue().isGeospatial() || property.getValue().isCollection()) {
+ valuable(jgen, property, Constants.VALUE);
+ } else if (property.getValue().isComplex()) {
+ for (Property cproperty : property.getValue().asComplex().get()) {
+ valuable(jgen, cproperty, cproperty.getName());
+ }
+ }
+
+ jgen.writeEndObject();
+ }
+}