diff --git a/emfjson-core/src/main/java/org/emfjson/EMFJs.java b/emfjson-core/src/main/java/org/emfjson/EMFJs.java index 946a6921..73757e36 100644 --- a/emfjson-core/src/main/java/org/emfjson/EMFJs.java +++ b/emfjson-core/src/main/java/org/emfjson/EMFJs.java @@ -84,13 +84,13 @@ public final class EMFJs { *

*

By default org.emfjson.jackson.databind.deser.DefaultTypeSerializer will be use

*/ - public static final String OPTION_TYPE_SERIALIZER = "OPTION_ID_SERIALIZER"; + public static final String OPTION_TYPE_SERIALIZER = "OPTION_TYPE_SERIALIZER"; /** * Specify the TypeDeserializer object to use during deserialization of object's type. *

*

By default org.emfjson.jackson.databind.deser.DefaultTypeDeserializer will be use

*/ - public static final String OPTION_TYPE_DESERIALIZER = "OPTION_ID_DESERIALIZER"; + public static final String OPTION_TYPE_DESERIALIZER = "OPTION_TYPE_DESERIALIZER"; /** * Specify the field name that will be use to denote the type of objects. *

By default eClass will be use

@@ -107,6 +107,11 @@ public final class EMFJs { *

By default _id will be use

*/ public static final String OPTION_ID_FIELD = "OPTION_ID_FIELD"; + /** + * Specify the date format that will be use to parse and write dates. + *

By default the date format is yyyy-MM-dd'T'HH:mm:ss

+ */ + public static final String OPTION_DATE_FORMAT = "OPTION_DATE_FORMAT"; private EMFJs() {} diff --git a/emfjson-jackson/src/main/java/org/emfjson/jackson/JacksonOptions.java b/emfjson-jackson/src/main/java/org/emfjson/jackson/JacksonOptions.java index 647a4d7c..3eada5a2 100644 --- a/emfjson-jackson/src/main/java/org/emfjson/jackson/JacksonOptions.java +++ b/emfjson-jackson/src/main/java/org/emfjson/jackson/JacksonOptions.java @@ -12,6 +12,7 @@ package org.emfjson.jackson; import org.eclipse.emf.ecore.EClass; +import org.emfjson.EMFJs; import org.emfjson.common.Options; import org.emfjson.handlers.URIHandler; import org.emfjson.jackson.databind.deser.DefaultTypeDeserializer; @@ -27,6 +28,8 @@ import org.emfjson.jackson.databind.ser.references.ReferenceAsObjectSerializer; import org.emfjson.jackson.databind.ser.references.ReferenceSerializer; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Map; @@ -40,6 +43,7 @@ public class JacksonOptions extends Options { public final IdDeserializer idDeserializer; public final TypeSerializer typeSerializer; public final TypeDeserializer typeDeserializer; + public final DateFormat dateFormat; protected JacksonOptions(JacksonOptions.Builder builder) { super(builder); @@ -50,6 +54,7 @@ protected JacksonOptions(JacksonOptions.Builder builder) { this.idDeserializer = builder.idDeserializer; this.typeSerializer = builder.typeSerializer; this.typeDeserializer = builder.typeDeserializer; + this.dateFormat = builder.dateFormat; } public static JacksonOptions from(Map options) { @@ -62,8 +67,9 @@ public static final class Builder extends Options.Builder { protected ReferenceDeserializer referenceDeserializer = new ReferenceAsObjectDeserializer(); protected IdSerializer idSerializer = new FragmentIdSerializer(); protected IdDeserializer idDeserializer = new FragmentIdDeserializer(); - private TypeSerializer typeSerializer = new DefaultTypeSerializer(); - private TypeDeserializer typeDeserializer = new DefaultTypeDeserializer(); + protected TypeSerializer typeSerializer = new DefaultTypeSerializer(); + protected TypeDeserializer typeDeserializer = new DefaultTypeDeserializer(); + protected DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); @Override public JacksonOptions build(Map options) { @@ -85,6 +91,7 @@ protected void init(Map options) { referenceDeserializer = objectValue(options, OPTION_REF_DESERIALIZER, referenceDeserializer); idSerializer = objectValue(options, OPTION_ID_SERIALIZER, idSerializer); idDeserializer = objectValue(options, OPTION_ID_DESERIALIZER, idDeserializer); + dateFormat = objectValue(options, EMFJs.OPTION_DATE_FORMAT, dateFormat); } @Override @@ -151,6 +158,12 @@ public Builder withTypeDeserializer(TypeDeserializer typeDeserializer) { this.typeDeserializer = typeDeserializer; return this; } + + public Builder withDateFormat(DateFormat dateFormat) { + this.dateFormat = dateFormat; + return this; + } + } } diff --git a/emfjson-jackson/src/main/java/org/emfjson/jackson/databind/deser/DateDeserializer.java b/emfjson-jackson/src/main/java/org/emfjson/jackson/databind/deser/DateDeserializer.java deleted file mode 100644 index ebcc9852..00000000 --- a/emfjson-jackson/src/main/java/org/emfjson/jackson/databind/deser/DateDeserializer.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2015 Guillaume Hillairet. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Guillaume Hillairet - initial API and implementation - * - */ -package org.emfjson.jackson.databind.deser; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; - -import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - -public class DateDeserializer extends JsonDeserializer { - - private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - - @Override - public Class handledType() { - return Date.class; - } - - @Override - public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - try { - return sdf.parse(p.getText()); - } catch (ParseException e) { - throw new IOException(e); - } - } - -} diff --git a/emfjson-jackson/src/main/java/org/emfjson/jackson/databind/ser/DateSerializer.java b/emfjson-jackson/src/main/java/org/emfjson/jackson/databind/ser/DateSerializer.java deleted file mode 100644 index f585ca50..00000000 --- a/emfjson-jackson/src/main/java/org/emfjson/jackson/databind/ser/DateSerializer.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 Guillaume Hillairet. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Guillaume Hillairet - initial API and implementation - * - */ -package org.emfjson.jackson.databind.ser; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.SerializerProvider; - -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; - -public class DateSerializer extends JsonSerializer { - - private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - - @Override - public void serialize(Date value, JsonGenerator jg, SerializerProvider provider) throws IOException { - jg.writeString(sdf.format(value)); - } - - @Override - public Class handledType() { - return Date.class; - } - -} diff --git a/emfjson-jackson/src/main/java/org/emfjson/jackson/module/EMFModule.java b/emfjson-jackson/src/main/java/org/emfjson/jackson/module/EMFModule.java index 32f150ec..e3556db4 100644 --- a/emfjson-jackson/src/main/java/org/emfjson/jackson/module/EMFModule.java +++ b/emfjson-jackson/src/main/java/org/emfjson/jackson/module/EMFModule.java @@ -19,7 +19,6 @@ import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.emfjson.jackson.JacksonOptions; -import org.emfjson.jackson.databind.deser.DateDeserializer; import org.emfjson.jackson.databind.deser.EObjectDeserializer; import org.emfjson.jackson.databind.deser.ResourceDeserializer; import org.emfjson.jackson.databind.ser.*; @@ -27,8 +26,6 @@ import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.module.SimpleModule; -import java.util.Date; - /** * Module implementation that allows serialization and deserialization of * EMF objects (EObject and Resource). @@ -62,14 +59,12 @@ public EMFModule(ResourceSet resourceSet, JacksonOptions options) { protected void configure() { addSerializer(new EObjectSerializer(options)); addSerializer(new ResourceSerializer()); - addSerializer(new DateSerializer()); addSerializer(Enumerator.class, new EnumeratorSerializer()); addSerializer(EEnumLiteral.class, new EnumeratorSerializer()); addSerializer(new EMapSerializer()); addSerializer(new EStringToStringMapEntrySerializer()); addDeserializer(EObject.class, new EObjectDeserializer(resourceSet, options)); addDeserializer(Resource.class, new ResourceDeserializer(resourceSet, options)); - addDeserializer(Date.class, new DateDeserializer()); } @Override @@ -79,7 +74,7 @@ public String getModuleName() { @Override public Version version() { - return new Version(0, 11, 0, "SNAPSHOT", "org.emfjson", "emfjson-jackson"); + return new Version(0, 12, 0, null, "org.emfjson", "emfjson-jackson"); } } diff --git a/emfjson-jackson/src/main/java/org/emfjson/jackson/resource/JsonResource.java b/emfjson-jackson/src/main/java/org/emfjson/jackson/resource/JsonResource.java index d28ddb71..970e20fe 100644 --- a/emfjson-jackson/src/main/java/org/emfjson/jackson/resource/JsonResource.java +++ b/emfjson-jackson/src/main/java/org/emfjson/jackson/resource/JsonResource.java @@ -10,18 +10,18 @@ */ package org.emfjson.jackson.resource; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.cfg.ContextAttributes; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.URIConverter; - import org.emfjson.jackson.JacksonOptions; import org.emfjson.jackson.module.EMFModule; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.cfg.ContextAttributes; - -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Collections; import java.util.Map; @@ -51,7 +51,9 @@ protected void doLoad(InputStream inputStream, Map options) throws IOExcep } else { final ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(new EMFModule(this.getResourceSet(), JacksonOptions.from(options))); + final JacksonOptions jacksonOptions = JacksonOptions.from(options); + mapper.setDateFormat(jacksonOptions.dateFormat); + mapper.registerModule(new EMFModule(this.getResourceSet(), jacksonOptions)); ContextAttributes attributes = ContextAttributes .getEmpty() @@ -79,6 +81,7 @@ protected void doSave(OutputStream outputStream, Map options) throws IOExc final ObjectMapper mapper = new ObjectMapper(); final JacksonOptions jacksonOptions = JacksonOptions.from(options); + mapper.setDateFormat(jacksonOptions.dateFormat); mapper.configure(SerializationFeature.INDENT_OUTPUT, jacksonOptions.indentOutput); mapper.registerModule(new EMFModule(this.getResourceSet(), jacksonOptions)); outputStream.write(mapper.writeValueAsBytes(this)); diff --git a/emfjson-jackson/src/test/java/org/emfjson/jackson/junit/support/TestSupport.java b/emfjson-jackson/src/test/java/org/emfjson/jackson/junit/support/TestSupport.java index f7e90e9b..b60d301a 100644 --- a/emfjson-jackson/src/test/java/org/emfjson/jackson/junit/support/TestSupport.java +++ b/emfjson-jackson/src/test/java/org/emfjson/jackson/junit/support/TestSupport.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.net.URL; +import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; @@ -52,6 +53,7 @@ public void setUp() { resourceSet.getURIConverter().getURIMap().put(baseURI, baseTestFilesFileDirectory); mapper.registerModule(new EMFModule(resourceSet, from(options))); + mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")); createDynamicModel(); }