Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
[#66] [jackson] Allow custom dates format
Browse files Browse the repository at this point in the history
  • Loading branch information
ghillairet committed Jul 26, 2015
1 parent 21c9554 commit 127bb96
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 94 deletions.
9 changes: 7 additions & 2 deletions emfjson-core/src/main/java/org/emfjson/EMFJs.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ public final class EMFJs {
* <p/>
* <p>By default org.emfjson.jackson.databind.deser.DefaultTypeSerializer will be use</p>
*/
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.
* <p/>
* <p>By default org.emfjson.jackson.databind.deser.DefaultTypeDeserializer will be use</p>
*/
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.
* <p>By default eClass will be use</p>
Expand All @@ -107,6 +107,11 @@ public final class EMFJs {
* <p>By default _id will be use</p>
*/
public static final String OPTION_ID_FIELD = "OPTION_ID_FIELD";
/**
* Specify the date format that will be use to parse and write dates.
* <p>By default the date format is yyyy-MM-dd'T'HH:mm:ss</p>
*/
public static final String OPTION_DATE_FORMAT = "OPTION_DATE_FORMAT";

private EMFJs() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -151,6 +158,12 @@ public Builder withTypeDeserializer(TypeDeserializer typeDeserializer) {
this.typeDeserializer = typeDeserializer;
return this;
}

public Builder withDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
return this;
}

}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
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.*;

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).
Expand Down Expand Up @@ -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
Expand All @@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down

0 comments on commit 127bb96

Please sign in to comment.