You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2021/07/28 11:20:25 UTC
[uima-uimaj] branch
feature/UIMA-6266-Clean-JSON-Wire-Format-for-CAS created (now d31473e)
This is an automated email from the ASF dual-hosted git repository.
rec pushed a change to branch feature/UIMA-6266-Clean-JSON-Wire-Format-for-CAS
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git.
at d31473e [UIMA-6266] Clean JSON Wire Format for CAS
This branch includes the following new commits:
new d31473e [UIMA-6266] Clean JSON Wire Format for CAS
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[uima-uimaj] 01/01: [UIMA-6266] Clean JSON Wire Format for CAS
Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rec pushed a commit to branch feature/UIMA-6266-Clean-JSON-Wire-Format-for-CAS
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git
commit d31473ecd3b26aaa48d7277116e13e2bcc3ebe43
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Sun Dec 13 14:09:38 2020 +0100
[UIMA-6266] Clean JSON Wire Format for CAS
- Some initial drafting
---
uimaj-json/pom.xml | 24 +
.../org/apache/uima/json/Json2CasDeserializer.java | 236 ++++++++++
.../org/apache/uima/json/Json2CasSerializer.java | 491 +++++++++++++++++++++
.../main/java/org/apache/uima/json/Json2Names.java | 30 ++
.../json/model/json2/Json2FeatureStructure.java | 93 ++++
.../apache/uima/json/model/json2/Json2Type.java | 79 ++++
.../json/Json2CasDeserializeSerializeTest.java | 61 +++
.../apache/uima/json/Json2DeserializerTest.java | 56 +++
.../org/apache/uima/json/Json2SerializerTest.java | 129 ++++++
.../Json2Deserializer/feature_structures_only.json | 23 +
.../resources/Json2Deserializer/text_only.json | 1 +
.../customAnnotationType/reference.json | 21 +
.../reference.json | 39 ++
.../reference.json | 40 ++
.../multipleViewsAndSofas/reference.json | 35 ++
15 files changed, 1358 insertions(+)
diff --git a/uimaj-json/pom.xml b/uimaj-json/pom.xml
index eef3abd..3aa6d78 100644
--- a/uimaj-json/pom.xml
+++ b/uimaj-json/pom.xml
@@ -54,6 +54,18 @@
</dependency>
<dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>${jackson.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-annotations</artifactId>
+ <version>${jackson.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.uima</groupId>
<artifactId>uimaj-test-util</artifactId>
<version>${project.parent.version}</version>
@@ -65,6 +77,18 @@
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.skyscreamer</groupId>
+ <artifactId>jsonassert</artifactId>
+ <version>1.5.0</version>
+ </dependency>
</dependencies>
<build>
diff --git a/uimaj-json/src/main/java/org/apache/uima/json/Json2CasDeserializer.java b/uimaj-json/src/main/java/org/apache/uima/json/Json2CasDeserializer.java
new file mode 100644
index 0000000..c04fe67
--- /dev/null
+++ b/uimaj-json/src/main/java/org/apache/uima/json/Json2CasDeserializer.java
@@ -0,0 +1,236 @@
+package org.apache.uima.json;
+
+import static com.fasterxml.jackson.core.JsonTokenId.ID_END_ARRAY;
+import static com.fasterxml.jackson.core.JsonTokenId.ID_END_OBJECT;
+import static com.fasterxml.jackson.core.JsonTokenId.ID_START_ARRAY;
+import static com.fasterxml.jackson.core.JsonTokenId.ID_START_OBJECT;
+import static com.fasterxml.jackson.core.JsonTokenId.ID_STRING;
+import static org.apache.uima.cas.CAS.FEATURE_BASE_NAME_LANGUAGE;
+import static org.apache.uima.cas.CAS.FEATURE_BASE_NAME_SOFAID;
+import static org.apache.uima.cas.CAS.FEATURE_BASE_NAME_SOFASTRING;
+import static org.apache.uima.cas.CAS.TYPE_NAME_INTEGER;
+import static org.apache.uima.cas.CAS.TYPE_NAME_SOFA;
+import static org.apache.uima.cas.CAS.TYPE_NAME_STRING;
+import static org.apache.uima.json.Json2Names.FEATURE_STRUCTURES_FIELD;
+import static org.apache.uima.json.Json2Names.FLAG_DOCUMENT_ANNOTATION;
+import static org.apache.uima.json.Json2Names.TYPES_FIELD;
+import static org.apache.uima.json.Json2Names.VIEWS_FIELD;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.json.model.json2.Json2FeatureStructure;
+import org.apache.uima.json.model.json2.Json2Type;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.type.TypeReference;
+
+public class Json2CasDeserializer
+{
+ private final JsonParser parser;
+
+ private Map<String, Json2Type> types;
+
+ private Map<String, CAS> views;
+
+ public Json2CasDeserializer(JsonParser aParser)
+ {
+ parser = aParser;
+ }
+
+ public void read(CAS aCas) throws IOException
+ {
+ views = new HashMap<>();
+
+ aCas.getViewIterator().forEachRemaining(view -> views.put(view.getViewName(), view));
+
+ boolean isFirst = true;
+
+ Map<String, Json2FeatureStructure> jfses = null;
+
+ while (parser.nextToken() != null) {
+ if (isFirst) {
+ isFirst = false;
+ switch (parser.currentTokenId()) {
+ case ID_STRING:
+ readDocumentText(aCas, parser.getValueAsString());
+ return;
+ case ID_START_ARRAY:
+ jfses = readFeatureStructuresAsArray(parser);
+ return;
+ case ID_START_OBJECT:
+ // In this case, we continue in the main loop and process the type system and
+ // feature structures if we find them.
+ parser.nextFieldName();
+ break;
+ default:
+ throw new IOException("JSON must start with an object, array or string value");
+ }
+ }
+
+ if (parser.currentTokenId() == ID_END_ARRAY
+ || parser.currentTokenId() == ID_END_OBJECT) {
+ break;
+ }
+
+ // If we get here, we are operating on an object-type representation of the full CAS
+ switch (parser.getValueAsString()) {
+ case TYPES_FIELD:
+ parser.nextValue();
+ types = readTypeSystem(parser);
+ break;
+ case VIEWS_FIELD:
+ readViews(aCas, parser);
+ break;
+ case FEATURE_STRUCTURES_FIELD:
+ switch (parser.nextToken().id()) {
+ case ID_START_ARRAY:
+ jfses = readFeatureStructuresAsArray(parser);
+ break;
+ case ID_START_OBJECT:
+ jfses = readFeatureStructuresAsObject(parser);
+ break;
+ default:
+ throw new IOException("Feature structures must be an object or array");
+ }
+ break;
+ }
+ }
+
+ if (jfses != null) {
+ for (Json2FeatureStructure jfs : jfses.values()) {
+ createFeatureStructure(aCas, jfs);
+ }
+ }
+ }
+
+ private void createFeatureStructure(CAS aCas, Json2FeatureStructure aJfs)
+ {
+ Type type = aCas.getTypeSystem().getType(aJfs.getType());
+
+ if (TYPE_NAME_SOFA.equals(type.getName())) {
+ CAS view = getView(aCas, (String) aJfs.getFeatures().get(FEATURE_BASE_NAME_SOFAID));
+ view.setDocumentText((String) aJfs.getFeatures().get(FEATURE_BASE_NAME_SOFASTRING));
+ view.setDocumentLanguage((String) aJfs.getFeatures().get(FEATURE_BASE_NAME_LANGUAGE));
+ return;
+ }
+
+ FeatureStructure fs = getFSCreationView(aCas, aJfs).createFS(type);
+
+ if (aJfs.getFlags() != null && aJfs.getFlags().contains(FLAG_DOCUMENT_ANNOTATION)) {
+ fs.getCAS().removeFsFromIndexes(fs.getCAS().getDocumentAnnotation());
+ }
+
+ for (Entry<String, Object> entry : aJfs.getFeatures().entrySet()) {
+ Feature feature = type.getFeatureByBaseName(entry.getKey());
+ if (feature.getRange().isPrimitive()) {
+ switch (feature.getRange().getName()) {
+ case TYPE_NAME_STRING:
+ fs.setStringValue(feature, (String) entry.getValue());
+ break;
+ case TYPE_NAME_INTEGER:
+ fs.setIntValue(feature, (Integer) entry.getValue());
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported primitive type [" + feature.getRange().getName() + "]");
+ }
+ }
+ }
+
+ addToIndexes(fs, aJfs);
+ }
+
+ private CAS getFSCreationView(CAS aCas, Json2FeatureStructure aJfs)
+ {
+ if (aJfs.getViews().isEmpty()) {
+ return null;
+ }
+
+ if (aJfs.getViews() != null) {
+ return getView(aCas, aJfs.getViews().iterator().next());
+ }
+
+ // FIXME: case when the views are not inline but separate or when FS is simply
+ // not indexed
+ throw new UnsupportedOperationException();
+ }
+
+ private CAS getView(CAS aCas, String aViewName)
+ {
+ CAS view = views.get(aViewName);
+ if (view == null) {
+ view = aCas.createView(aViewName);
+ views.put(aViewName, view);
+ }
+ return view;
+ }
+
+ private void addToIndexes(FeatureStructure aFS, Json2FeatureStructure aJfs)
+ {
+ if (aJfs.getViews() != null) {
+ for (String viewName : aJfs.getViews()) {
+ getView(aFS.getCAS(), viewName).addFsToIndexes(aFS);
+ }
+ }
+ }
+
+ private void readViews(CAS aCas, JsonParser aParser) throws IOException
+ {
+ while (aParser.currentTokenId() != ID_END_OBJECT) {
+ aParser.nextFieldName();
+ String viewName = aParser.readValueAs(String.class);
+ aCas.createView(viewName);
+ }
+ }
+
+ private Map<String, Json2Type> readTypeSystem(JsonParser aParser) throws IOException
+ {
+ return aParser.readValueAs(TYPES_MAP_TYPE_REF);
+ }
+
+ private Map<String, Json2FeatureStructure> readFeatureStructuresAsObject(JsonParser aParser)
+ throws IOException
+ {
+ Map<String, Json2FeatureStructure> jfses = new LinkedHashMap<>();
+ while (aParser.currentTokenId() != ID_END_OBJECT) {
+ String id = aParser.nextFieldName();
+ Json2FeatureStructure jfs = aParser.readValueAs(Json2FeatureStructure.class);
+ jfs.setId(id);
+ jfses.put(id, jfs);
+ }
+
+ return jfses;
+ }
+
+ private Map<String, Json2FeatureStructure> readFeatureStructuresAsArray(JsonParser aParser)
+ throws IOException
+ {
+ Map<String, Json2FeatureStructure> jfses = new LinkedHashMap<>();
+ parser.nextValue();
+ while (aParser.currentTokenId() != ID_END_ARRAY) {
+ Json2FeatureStructure jfs = aParser.readValueAs(Json2FeatureStructure.class);
+ aParser.nextToken();
+ jfses.put(jfs.getId(), jfs);
+ }
+
+ return jfses;
+ }
+
+ private void readDocumentText(CAS aCas, String aString)
+ {
+ aCas.setDocumentText(aString);
+ }
+
+ public static final TypeReference<LinkedHashMap<String, Json2Type>> TYPES_MAP_TYPE_REF = //
+ new TypeReference<LinkedHashMap<String, Json2Type>>()
+ {
+ };
+}
diff --git a/uimaj-json/src/main/java/org/apache/uima/json/Json2CasSerializer.java b/uimaj-json/src/main/java/org/apache/uima/json/Json2CasSerializer.java
new file mode 100644
index 0000000..f21caa7
--- /dev/null
+++ b/uimaj-json/src/main/java/org/apache/uima/json/Json2CasSerializer.java
@@ -0,0 +1,491 @@
+package org.apache.uima.json;
+
+
+import static com.fasterxml.jackson.core.JsonEncoding.UTF8;
+import static java.util.Arrays.asList;
+import static java.util.Arrays.sort;
+import static java.util.Collections.sort;
+import static java.util.Collections.unmodifiableSet;
+import static java.util.Comparator.comparing;
+import static java.util.stream.Collectors.toList;
+import static org.apache.uima.json.Json2CasSerializer.FeatureStructuresMode.AS_ARRAY;
+import static org.apache.uima.json.Json2CasSerializer.ViewsMode.INLINE;
+import static org.apache.uima.json.Json2CasSerializer.ViewsMode.SEPARATE;
+import static org.apache.uima.json.Json2Names.COMPONENT_TYPE_FIELD;
+import static org.apache.uima.json.Json2Names.FEATURE_STRUCTURES_FIELD;
+import static org.apache.uima.json.Json2Names.FLAG_DOCUMENT_ANNOTATION;
+import static org.apache.uima.json.Json2Names.ID_FIELD;
+import static org.apache.uima.json.Json2Names.REF;
+import static org.apache.uima.json.Json2Names.SUPER_TYPE_FIELD;
+import static org.apache.uima.json.Json2Names.TYPES_FIELD;
+import static org.apache.uima.json.Json2Names.TYPE_FIELD;
+import static org.apache.uima.json.Json2Names.VIEWS_FIELD;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.StreamSupport;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.impl.CASImpl;
+import org.apache.uima.jcas.cas.TOP;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class Json2CasSerializer
+{
+ public static enum FeatureStructuresMode
+ {
+ AS_OBJECT, AS_ARRAY
+ }
+
+ public static enum ViewsMode
+ {
+ SEPARATE, INLINE
+ }
+
+ private final Set<String> BUILT_IN_TYPES = unmodifiableSet(new HashSet<>(asList(
+ CAS.TYPE_NAME_ANNOTATION,
+ CAS.TYPE_NAME_ANNOTATION_BASE,
+ CAS.TYPE_NAME_ARRAY_BASE,
+ CAS.TYPE_NAME_BOOLEAN,
+ CAS.TYPE_NAME_BOOLEAN_ARRAY,
+ CAS.TYPE_NAME_BYTE,
+ CAS.TYPE_NAME_BYTE_ARRAY,
+ CAS.TYPE_NAME_DOCUMENT_ANNOTATION,
+ CAS.TYPE_NAME_DOUBLE,
+ CAS.TYPE_NAME_DOUBLE_ARRAY,
+ CAS.TYPE_NAME_EMPTY_FLOAT_LIST,
+ CAS.TYPE_NAME_EMPTY_FS_LIST,
+ CAS.TYPE_NAME_EMPTY_INTEGER_LIST,
+ CAS.TYPE_NAME_EMPTY_STRING_LIST,
+ CAS.TYPE_NAME_FLOAT,
+ CAS.TYPE_NAME_FLOAT_ARRAY,
+ CAS.TYPE_NAME_FLOAT_LIST,
+ CAS.TYPE_NAME_FS_ARRAY,
+ CAS.TYPE_NAME_FS_LIST,
+ CAS.TYPE_NAME_INTEGER,
+ CAS.TYPE_NAME_INTEGER_ARRAY,
+ CAS.TYPE_NAME_INTEGER_LIST,
+ CAS.TYPE_NAME_LIST_BASE,
+ CAS.TYPE_NAME_LONG,
+ CAS.TYPE_NAME_LONG_ARRAY,
+ CAS.TYPE_NAME_NON_EMPTY_FLOAT_LIST,
+ CAS.TYPE_NAME_NON_EMPTY_FS_LIST,
+ CAS.TYPE_NAME_NON_EMPTY_INTEGER_LIST,
+ CAS.TYPE_NAME_NON_EMPTY_STRING_LIST,
+ CAS.TYPE_NAME_SHORT,
+ CAS.TYPE_NAME_SHORT_ARRAY,
+ CAS.TYPE_NAME_SOFA,
+ CAS.TYPE_NAME_STRING,
+ CAS.TYPE_NAME_STRING_ARRAY,
+ CAS.TYPE_NAME_STRING_LIST,
+ CAS.TYPE_NAME_TOP)));
+
+ private JsonGenerator jg;
+
+ private FeatureStructuresMode featureStructuresMode = AS_ARRAY;
+
+ private ViewsMode viewsMode = INLINE;
+
+ private Supplier<Function<FeatureStructure, String>> idRefGeneratorSupplier;
+ private Function<FeatureStructure, String> idRefGenerator;
+ private Map<FeatureStructure, String> idRefCache;
+
+ private Supplier<Function<Type, String>> typeRefGeneratorSupplier;
+ private Function<Type, String> typeRefGenerator;
+ private Map<Type, String> typeRefCache;
+
+ private Map<FeatureStructure, Set<String>> fsToViewsCache;
+
+ public Json2CasSerializer(JsonGenerator aJg)
+ {
+ jg = aJg;
+ setIdRefGeneratorSupplier(SequentialIdRefGenerator::new);
+ setTypeRefGeneratorSupplier(FullyQualifiedTypeRefGenerator::new);
+ }
+
+ public void setViewsMode(ViewsMode aViewsMode)
+ {
+ viewsMode = aViewsMode;
+ }
+
+ public ViewsMode getViewsMode()
+ {
+ return viewsMode;
+ }
+
+ public FeatureStructuresMode getFeatureStructuresMode()
+ {
+ return featureStructuresMode;
+ }
+
+ public void setFeatureStructuresMode(FeatureStructuresMode aFeatureStructuresMode)
+ {
+ featureStructuresMode = aFeatureStructuresMode;
+ }
+
+ public void setIdRefGeneratorSupplier(
+ Supplier<Function<FeatureStructure, String>> aIdRefGeneratorSupplier)
+ {
+ idRefGeneratorSupplier = aIdRefGeneratorSupplier;
+ }
+
+ public void setTypeRefGeneratorSupplier(
+ Supplier<Function<Type, String>> aTypeRefGeneratorSupplier)
+ {
+ typeRefGeneratorSupplier = aTypeRefGeneratorSupplier;
+ }
+
+ public void write(CAS aCas) throws IOException
+ {
+ idRefGenerator = idRefGeneratorSupplier.get();
+ typeRefGenerator = typeRefGeneratorSupplier.get();
+
+ idRefCache = new HashMap<>();
+ typeRefCache = new HashMap<>();
+ fsToViewsCache = new IdentityHashMap<>();
+
+ jg.writeStartObject(aCas);
+
+ writeTypeSystem(aCas.getTypeSystem());
+
+ List<CAS> views = new ArrayList<>();
+ aCas.getViewIterator().forEachRemaining(views::add);
+ if (!views.isEmpty()) {
+ sort(views, comparing(CAS::getViewName));
+
+ if (viewsMode == SEPARATE) {
+ jg.writeObjectFieldStart(VIEWS_FIELD);
+ for (CAS view : views) {
+ jg.writeFieldName(view.getViewName());
+ writeView(view);
+ }
+ jg.writeEndObject();
+ }
+
+ if (viewsMode == INLINE) {
+ for (CAS view : views) {
+ for (FeatureStructure fs : view.select()) {
+ fsToViewsCache.computeIfAbsent(fs, _fs -> new HashSet<>())
+ .add(view.getViewName());
+ }
+ }
+ }
+ }
+
+ Set<FeatureStructure> allFSes = findAllFeatureStructures(aCas);
+
+ if (!allFSes.isEmpty()) {
+ switch (featureStructuresMode) {
+ case AS_ARRAY:
+ jg.writeArrayFieldStart(FEATURE_STRUCTURES_FIELD);
+ for (FeatureStructure fs : allFSes) {
+ jg.writeStartObject(fs);
+ jg.writeStringField(ID_FIELD, fsRef(fs));
+ writeFeatureStructure(fs);
+ jg.writeEndObject();
+ }
+ jg.writeEndArray();
+ break;
+ case AS_OBJECT:
+ jg.writeObjectFieldStart(FEATURE_STRUCTURES_FIELD);
+ for (FeatureStructure fs : allFSes) {
+ jg.writeFieldName(fsRef(fs));
+ jg.writeStartObject(fs);
+ writeFeatureStructure(fs);
+ jg.writeEndObject();
+ }
+ jg.writeEndObject();
+ break;
+ default:
+ throw new IOException("Unsupported feature structures serialization mode: ["
+ + featureStructuresMode + "]");
+ }
+ }
+
+ jg.writeEndObject();
+ }
+
+ private Set<FeatureStructure> findAllFeatureStructures(CAS aCas)
+ {
+ Set<FeatureStructure> allFSes = new LinkedHashSet<>();
+ ((CASImpl) aCas).walkReachablePlusFSsSorted(allFSes::add, null, null, null);
+ return allFSes;
+ }
+
+ private String fsRef(FeatureStructure aFs)
+ {
+ return idRefCache.computeIfAbsent(aFs, idRefGenerator);
+ }
+
+ private String typeRef(Type aType) {
+ return typeRefCache.computeIfAbsent(aType, typeRefGenerator);
+ }
+
+ private void writeFeatureStructure(FeatureStructure aFs) throws IOException
+ {
+ Type type = aFs.getType();
+ jg.writeStringField(TYPE_FIELD, typeRef(type));
+
+ if (viewsMode == INLINE) {
+ Set<String> views = fsToViewsCache.get(aFs);
+
+ if (views != null && !views.isEmpty()) {
+ String[] viewsArray = views.toArray(new String[views.size()]);
+ sort(viewsArray);
+ jg.writeArrayFieldStart(VIEWS_FIELD);
+ for (String view : viewsArray) {
+ jg.writeString(view);
+ }
+ jg.writeEndArray();
+ }
+ }
+
+ List<String> flags = new ArrayList<>();
+ if (((CASImpl) aFs.getCAS()).getDocumentAnnotationNoCreate() == aFs) {
+ flags.add(FLAG_DOCUMENT_ANNOTATION);
+ }
+
+ if (!flags.isEmpty()) {
+ jg.writeArrayFieldStart(Json2Names.FLAGS_FIELD);
+ for (String flag : flags) {
+ jg.writeString(flag);
+ }
+ jg.writeEndArray();
+ }
+
+ for (Feature feature : type.getFeatures()) {
+ writeFeature(aFs, feature);
+ }
+ }
+
+ private void writeFeature(FeatureStructure aFs, Feature aFeature) throws IOException
+ {
+ if (!aFeature.getRange().isPrimitive()) {
+ FeatureStructure target = aFs.getFeatureValue(aFeature);
+ if (target != null) {
+ jg.writeFieldName(REF + aFeature.getShortName());
+ jg.writeString(fsRef(aFs.getFeatureValue(aFeature)));
+ }
+ return;
+ }
+
+ if (aFeature.getRange().isStringOrStringSubtype()) {
+ String value = aFs.getStringValue(aFeature);
+ if (value != null) {
+ jg.writeFieldName(aFeature.getShortName());
+ jg.writeString(value);
+ }
+
+ return;
+ }
+
+ jg.writeFieldName(aFeature.getShortName());
+ String rangeTypeName = aFeature.getRange().getName();
+ switch (rangeTypeName) {
+ case CAS.TYPE_NAME_INTEGER:
+ jg.writeNumber(aFs.getIntValue(aFeature));
+ break;
+ default:
+ throw new IOException("Unsupported primitive type [" + rangeTypeName + "]");
+ }
+ }
+
+ private void writeView(CAS aView) throws IOException
+ {
+ jg.writeStartArray();
+
+ jg.writeString(fsRef(aView.getSofa()));
+ for (TOP fs : aView.getIndexedFSs()) {
+ jg.writeString(fsRef(fs));
+ }
+
+ jg.writeEndArray();
+ }
+
+ private void writeTypeSystem(TypeSystem aTypeSystem) throws IOException
+ {
+ List<Type> types = StreamSupport.stream(aTypeSystem.spliterator(), false)
+ .sorted(comparing(Type::getName))
+ .filter(type -> !BUILT_IN_TYPES.contains(type.getName()))
+ .collect(toList());
+
+ if (types.isEmpty()) {
+ return;
+ }
+
+ jg.writeFieldName(TYPES_FIELD);
+
+ jg.writeStartObject(aTypeSystem);
+
+ for (Type type : types) {
+ writeType(aTypeSystem, type);
+ }
+
+ jg.writeEndObject();
+ }
+
+ private void writeType(TypeSystem aTypeSystem, Type aType) throws IOException
+ {
+ jg.writeFieldName(typeRef(aType));
+
+ jg.writeStartObject(aType);
+
+ if (!typeRef(aType).equals(aType.getName())) {
+ jg.writeStringField(Json2Names.NAME_FIELD, aType.getName());
+ }
+
+ Type parent = aTypeSystem.getParent(aType);
+ if (parent != null) {
+ jg.writeStringField(SUPER_TYPE_FIELD, parent.getName());
+ }
+
+ if (aType.getComponentType() != null) {
+ jg.writeStringField(COMPONENT_TYPE_FIELD, aType.getComponentType().getName());
+ }
+
+ List<Feature> newFeatures = aType.getFeatures().stream().filter(f -> f.getDomain() == aType)
+ .collect(toList());
+ if (!newFeatures.isEmpty()) {
+ for (Feature feature : newFeatures) {
+ jg.writeStringField(feature.getShortName(), feature.getRange().getName());
+ }
+ }
+
+ jg.writeEndObject();
+ }
+
+ public static Builder builder()
+ {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private Supplier<Function<FeatureStructure, String>> idRefGeneratorSupplier;
+ private Supplier<Function<Type, String>> typeRefGeneratorSupplier;
+ private FeatureStructuresMode featureStructuresMode = AS_ARRAY;
+ private ViewsMode viewsMode = INLINE;
+
+ public Builder()
+ {
+ setIdRefGeneratorSupplier(SequentialIdRefGenerator::new);
+ setTypeRefGeneratorSupplier(FullyQualifiedTypeRefGenerator::new);
+ }
+
+ public Builder setFeatureStructuresMode(FeatureStructuresMode aFeatureStructuresMode)
+ {
+ featureStructuresMode = aFeatureStructuresMode;
+ return this;
+ }
+
+ public Builder setViewsMode(ViewsMode aViewsMode)
+ {
+ viewsMode = aViewsMode;
+ return this;
+ }
+
+ public Builder setIdRefGeneratorSupplier(
+ Supplier<Function<FeatureStructure, String>> aIdRefGeneratorSupplier)
+ {
+ idRefGeneratorSupplier = aIdRefGeneratorSupplier;
+ return this;
+ }
+
+ public Builder setTypeRefGeneratorSupplier(
+ Supplier<Function<Type, String>> aTypeRefGeneratorSupplier)
+ {
+ typeRefGeneratorSupplier = aTypeRefGeneratorSupplier;
+ return this;
+ }
+
+ public Json2CasSerializer build(JsonGenerator jg) {
+ Json2CasSerializer ser = new Json2CasSerializer(jg);
+ ser.setFeatureStructuresMode(featureStructuresMode);
+ ser.setViewsMode(viewsMode);
+ ser.setIdRefGeneratorSupplier(idRefGeneratorSupplier);
+ ser.setTypeRefGeneratorSupplier(typeRefGeneratorSupplier);
+ return ser;
+ }
+
+ public void write(CAS aCas, File aTargetFile) throws IOException
+ {
+ JsonFactory jsonFactory = new JsonFactory();
+ jsonFactory.setCodec(new ObjectMapper());
+ try (JsonGenerator jg = jsonFactory.createGenerator(aTargetFile, UTF8)
+ .useDefaultPrettyPrinter()) {
+ Json2CasSerializer ser = build(jg);
+ ser.write(aCas);
+ }
+ }
+ }
+
+ public static void write(CAS aCas, File aTargetFile) throws IOException
+ {
+ JsonFactory jsonFactory = new JsonFactory();
+ jsonFactory.setCodec(new ObjectMapper());
+ try (JsonGenerator jg = jsonFactory.createGenerator(aTargetFile, UTF8)
+ .useDefaultPrettyPrinter()) {
+ Json2CasSerializer ser = new Json2CasSerializer(jg);
+ ser.write(aCas);
+ }
+ }
+
+ public static class SequentialIdRefGenerator implements Function<FeatureStructure, String>
+ {
+ private int nextId = 0;
+
+ @Override
+ public String apply(FeatureStructure aT)
+ {
+ return String.valueOf(nextId++);
+ }
+ }
+
+ public static class FullyQualifiedTypeRefGenerator implements Function<Type, String>
+ {
+ @Override
+ public String apply(Type aType)
+ {
+ return aType.getName();
+ }
+ }
+
+ public static class ShortTypeRefGenerator implements Function<Type, String>
+ {
+ private Set<String> usedNames = new HashSet<>();
+
+ @Override
+ public String apply(Type aType)
+ {
+ if (!usedNames.contains(aType.getShortName())) {
+ usedNames.add(aType.getShortName());
+ return aType.getShortName();
+ }
+
+ int n = 1;
+ String newName;
+ while (usedNames.contains(newName = aType.getShortName() + "-" + n)) {
+ n++;
+ }
+
+ usedNames.add(newName);
+ return newName;
+ }
+ }
+}
diff --git a/uimaj-json/src/main/java/org/apache/uima/json/Json2Names.java b/uimaj-json/src/main/java/org/apache/uima/json/Json2Names.java
new file mode 100644
index 0000000..856ee89
--- /dev/null
+++ b/uimaj-json/src/main/java/org/apache/uima/json/Json2Names.java
@@ -0,0 +1,30 @@
+package org.apache.uima.json;
+
+public class Json2Names
+{
+ private static final String SPECIAL = "%";
+
+ public static final String TYPE_FIELD = SPECIAL + "TYPE";
+
+ public static final String TYPES_FIELD = SPECIAL + "TYPES";
+
+ public static final String FEATURES_FIELD = SPECIAL + "FEATURES";
+
+ public static final String VIEWS_FIELD = SPECIAL + "VIEWS";
+
+ public static final String FEATURE_STRUCTURES_FIELD = SPECIAL + "FEATURE_STRUCTURES";
+
+ public static final String REF = "@";
+
+ public static final String NAME_FIELD = SPECIAL + "NAME";
+
+ public static final String SUPER_TYPE_FIELD = SPECIAL + "SUPER_TYPE";
+
+ public static final String COMPONENT_TYPE_FIELD = SPECIAL + "COMPONENT_TYPE";
+
+ public static final String ID_FIELD = SPECIAL + "ID";
+
+ public static final String FLAGS_FIELD = SPECIAL + "FLAGS";
+
+ public static final String FLAG_DOCUMENT_ANNOTATION = "DocumentAnnotation";
+}
diff --git a/uimaj-json/src/main/java/org/apache/uima/json/model/json2/Json2FeatureStructure.java b/uimaj-json/src/main/java/org/apache/uima/json/model/json2/Json2FeatureStructure.java
new file mode 100644
index 0000000..20a89d9
--- /dev/null
+++ b/uimaj-json/src/main/java/org/apache/uima/json/model/json2/Json2FeatureStructure.java
@@ -0,0 +1,93 @@
+package org.apache.uima.json.model.json2;
+
+import static org.apache.uima.cas.CAS.TYPE_NAME_ANNOTATION;
+import static org.apache.uima.json.Json2Names.ID_FIELD;
+import static org.apache.uima.json.Json2Names.TYPE_FIELD;
+import static org.apache.uima.json.Json2Names.VIEWS_FIELD;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+
+import org.apache.uima.json.Json2Names;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+
+@JsonPropertyOrder(value = { "id", "type", "views" })
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class Json2FeatureStructure
+{
+ @JsonProperty(value = ID_FIELD, required = false)
+ private String id;
+
+ @JsonProperty(value = TYPE_FIELD, required = true, defaultValue = TYPE_NAME_ANNOTATION)
+ private String type;
+
+ @JsonProperty(value = Json2Names.FLAGS_FIELD, required = false)
+ private LinkedHashSet<String> flags;
+
+ @JsonProperty(value = VIEWS_FIELD, required = false)
+ private LinkedHashSet<String> views;
+
+ private Map<String, Object> features = new LinkedHashMap<>();
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public void setId(String aId)
+ {
+ id = aId;
+ }
+
+ public String getType()
+ {
+ return type;
+ }
+
+ public void setType(String aType)
+ {
+ type = aType;
+ }
+
+ public LinkedHashSet<String> getViews()
+ {
+ return views;
+ }
+
+ public void setViews(LinkedHashSet<String> aViews)
+ {
+ views = aViews;
+ }
+
+ public LinkedHashSet<String> getFlags()
+ {
+ return flags;
+ }
+
+ public void setFlags(LinkedHashSet<String> aFlags)
+ {
+ flags = aFlags;
+ }
+
+ @JsonAnySetter
+ public void setFeature(String name, Object value)
+ {
+ if (name.startsWith(Json2Names.REF)) {
+ name = name.substring(1);
+ }
+
+ features.put(name, value);
+ }
+
+ @JsonAnyGetter
+ public Map<String, Object> getFeatures()
+ {
+ return features;
+ }
+}
diff --git a/uimaj-json/src/main/java/org/apache/uima/json/model/json2/Json2Type.java b/uimaj-json/src/main/java/org/apache/uima/json/model/json2/Json2Type.java
new file mode 100644
index 0000000..2f5f51a
--- /dev/null
+++ b/uimaj-json/src/main/java/org/apache/uima/json/model/json2/Json2Type.java
@@ -0,0 +1,79 @@
+package org.apache.uima.json.model.json2;
+
+import static org.apache.uima.cas.CAS.TYPE_NAME_ANNOTATION;
+import static org.apache.uima.json.Json2Names.COMPONENT_TYPE_FIELD;
+import static org.apache.uima.json.Json2Names.NAME_FIELD;
+import static org.apache.uima.json.Json2Names.SUPER_TYPE_FIELD;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+
+@JsonPropertyOrder(value = { "name", "parent", "componentType" })
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class Json2Type
+{
+ @JsonProperty(value = NAME_FIELD, required = false)
+ private String name;
+
+ @JsonProperty(value = SUPER_TYPE_FIELD, required = true, defaultValue = TYPE_NAME_ANNOTATION)
+ private String parent;
+
+ @JsonProperty(value = COMPONENT_TYPE_FIELD, required = false)
+ private String componentType;
+
+ private Map<String, String> features = new LinkedHashMap<>();
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String aName)
+ {
+ name = aName;
+ }
+
+ public String getParent()
+ {
+ return parent;
+ }
+
+ public void setParent(String aParent)
+ {
+ parent = aParent;
+ }
+
+ public String getComponentType()
+ {
+ return componentType;
+ }
+
+ public void setComponentType(String aComponentType)
+ {
+ componentType = aComponentType;
+ }
+
+ @JsonAnySetter
+ public void setProperty(String name, Object value)
+ {
+ if (value instanceof String) {
+ features.put(name, (String) value);
+ return;
+ }
+
+ throw new IllegalArgumentException("Feature type names must be strings");
+ }
+
+ @SuppressWarnings("unchecked")
+ @JsonAnyGetter
+ public Map<String, Object> getProperties()
+ {
+ return (Map) features;
+ }
+}
diff --git a/uimaj-json/src/test/java/org/apache/uima/json/Json2CasDeserializeSerializeTest.java b/uimaj-json/src/test/java/org/apache/uima/json/Json2CasDeserializeSerializeTest.java
new file mode 100644
index 0000000..38086bd
--- /dev/null
+++ b/uimaj-json/src/test/java/org/apache/uima/json/Json2CasDeserializeSerializeTest.java
@@ -0,0 +1,61 @@
+package org.apache.uima.json;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Arrays.asList;
+import static org.apache.uima.util.CasCreationUtils.createCas;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.contentOf;
+import java.io.File;
+import org.apache.uima.cas.CAS;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@RunWith(value = Parameterized.class)
+public class Json2CasDeserializeSerializeTest
+{
+ @Parameters(name = "{index}: running on file {0}")
+ public static Iterable<File> tsvFiles()
+ {
+ return asList(new File("src/test/resources/Json2SerializerTest/")
+ .listFiles(file -> file.isDirectory()));
+ }
+
+ private CAS cas;
+ private File referenceFolder;
+ private File referenceFile;
+ private File outputFile;
+ private JsonFactory jsonFactory;
+
+ public Json2CasDeserializeSerializeTest(File aFolder) throws Exception
+ {
+ referenceFolder = aFolder;
+ referenceFile = new File(referenceFolder, "reference.json");
+ outputFile = new File("target/test-output/" + getClass().getSimpleName() + "/"
+ + referenceFolder.getName() + "/output.json");
+ outputFile.getParentFile().mkdirs();
+
+ cas = createCas();
+
+ jsonFactory = new JsonFactory();
+ jsonFactory.setCodec(new ObjectMapper());
+ }
+
+
+ @Test
+ public void testReadWrite() throws Exception
+ {
+ Json2CasDeserializer deser = new Json2CasDeserializer(
+ jsonFactory.createParser(referenceFile));
+ deser.read(cas);
+
+ Json2CasSerializer.builder().write(cas, outputFile);
+
+ assertThat(contentOf(outputFile, UTF_8)).isEqualTo(contentOf(referenceFile, UTF_8));
+ //assertEquals(contentOf(referenceFile, UTF_8), contentOf(outputFile, UTF_8), STRICT);
+ }
+}
diff --git a/uimaj-json/src/test/java/org/apache/uima/json/Json2DeserializerTest.java b/uimaj-json/src/test/java/org/apache/uima/json/Json2DeserializerTest.java
new file mode 100644
index 0000000..d1e924b
--- /dev/null
+++ b/uimaj-json/src/test/java/org/apache/uima/json/Json2DeserializerTest.java
@@ -0,0 +1,56 @@
+package org.apache.uima.json;
+
+import static org.apache.uima.util.CasCreationUtils.createCas;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+
+import org.apache.uima.cas.CAS;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class Json2DeserializerTest
+{
+ private static CAS cas;
+
+ private JsonFactory jsonFactory;
+
+ @BeforeClass
+ public static void setupOnce() throws Exception
+ {
+ cas = createCas();
+ }
+
+ @Before
+ public void setup() throws Exception
+ {
+ jsonFactory = new JsonFactory();
+ jsonFactory.setCodec(new ObjectMapper());
+ }
+
+ @Test
+ public void thatQuotedStringCanBeParsed() throws Exception
+ {
+ Json2CasDeserializer deser = new Json2CasDeserializer(
+ jsonFactory.createParser(new File("src/test/resources/Json2Deserializer/text_only.json")));
+
+ deser.read(cas);
+
+ assertThat(cas.getDocumentText()).isEqualTo("Hello world.");
+ }
+
+ @Test
+ public void thatFeatureStructureArrayCanBeParsed() throws Exception
+ {
+ Json2CasDeserializer deser = new Json2CasDeserializer(
+ jsonFactory.createParser(new File("src/test/resources/Json2Deserializer/feature_structures_only.json")));
+
+ deser.read(cas);
+
+ assertThat(cas.getDocumentText()).isEqualTo("Hello world.");
+ }
+}
diff --git a/uimaj-json/src/test/java/org/apache/uima/json/Json2SerializerTest.java b/uimaj-json/src/test/java/org/apache/uima/json/Json2SerializerTest.java
new file mode 100644
index 0000000..99b909e
--- /dev/null
+++ b/uimaj-json/src/test/java/org/apache/uima/json/Json2SerializerTest.java
@@ -0,0 +1,129 @@
+package org.apache.uima.json;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.apache.uima.UIMAFramework.getResourceSpecifierFactory;
+import static org.apache.uima.cas.CAS.TYPE_NAME_ANNOTATION;
+import static org.apache.uima.json.Json2CasSerializer.ViewsMode.INLINE;
+import static org.apache.uima.json.Json2CasSerializer.ViewsMode.SEPARATE;
+import static org.apache.uima.util.CasCreationUtils.createCas;
+import static org.assertj.core.api.Assertions.contentOf;
+import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
+import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
+
+import java.io.File;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.resource.metadata.TypeDescription;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.rules.TestName;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class Json2SerializerTest
+{
+ public @Rule TemporaryFolder temp = new TemporaryFolder();
+ public @Rule TestName name = new TestName();
+
+ private JsonFactory jsonFactory;
+
+ private File outputFile;
+ private File referenceFile;
+
+ @Before
+ public void setup() throws Exception
+ {
+ jsonFactory = new JsonFactory();
+ jsonFactory.setCodec(new ObjectMapper());
+
+ outputFile = new File("target/test-output/" + getClass().getSimpleName() + "/"
+ + name.getMethodName() + "/output.json");
+ outputFile.getParentFile().mkdirs();
+
+ referenceFile = new File("src/test/resources/" + getClass().getSimpleName() + "/"
+ + name.getMethodName() + "/reference.json");
+ }
+
+ @Test
+ public void multipleViewsAndSofas() throws Exception
+ {
+ CAS cas = createCas();
+ CAS firstView = cas;
+ firstView.setDocumentText("First view");
+ CAS secondView = firstView.createView("secondView");
+ secondView.setDocumentText("Second view");
+
+ Json2CasSerializer.write(cas, outputFile);
+
+ assertEquals(contentOf(referenceFile, UTF_8), contentOf(outputFile, UTF_8), STRICT);
+ }
+
+ @Test
+ public void featureStructureIndexedInMultipleViewsInline() throws Exception
+ {
+ CAS cas = createCas();
+ FeatureStructure fs = cas.createFS(cas.getTypeSystem().getTopType());
+
+ CAS firstView = cas;
+ firstView.setDocumentText("First view");
+ firstView.addFsToIndexes(fs);
+
+ CAS secondView = cas.createView("secondView");
+ secondView.setDocumentText("Second view");
+ secondView.addFsToIndexes(fs);
+
+ Json2CasSerializer.builder() //
+ .setViewsMode(INLINE) //
+ .write(cas, outputFile);
+
+ assertEquals(contentOf(referenceFile, UTF_8), contentOf(outputFile, UTF_8), STRICT);
+ }
+
+ @Test
+ public void featureStructureIndexedInMultipleViewsSeparate() throws Exception
+ {
+ CAS cas = createCas();
+ FeatureStructure fs = cas.createFS(cas.getTypeSystem().getTopType());
+
+ CAS firstView = cas;
+ firstView.setDocumentText("First view");
+ firstView.addFsToIndexes(fs);
+
+ CAS secondView = cas.createView("secondView");
+ secondView.setDocumentText("Second view");
+ secondView.addFsToIndexes(fs);
+
+ Json2CasSerializer.builder() //
+ .setViewsMode(SEPARATE) //
+ .write(cas, outputFile);
+
+ assertEquals(contentOf(referenceFile, UTF_8), contentOf(outputFile, UTF_8), STRICT);
+ }
+
+ @Test
+ public void customAnnotationType() throws Exception
+ {
+ String customTypeName = "custom.Annotation";
+
+ TypeSystemDescription tsd = getResourceSpecifierFactory().createTypeSystemDescription();
+ TypeDescription customTypeDesc = tsd.addType(customTypeName, "", TYPE_NAME_ANNOTATION);
+ customTypeDesc.addFeature("value", "", CAS.TYPE_NAME_STRING);
+
+ CAS cas = createCas(tsd, null, null, null);
+
+ Type customType = cas.getTypeSystem().getType(customTypeName);
+ AnnotationFS fs = cas.createAnnotation(customType, 0, 10);
+ cas.addFsToIndexes(fs);
+
+ Json2CasSerializer.write(cas, outputFile);
+
+ assertEquals(contentOf(referenceFile, UTF_8), contentOf(outputFile, UTF_8), STRICT);
+ }
+}
diff --git a/uimaj-json/src/test/resources/Json2Deserializer/feature_structures_only.json b/uimaj-json/src/test/resources/Json2Deserializer/feature_structures_only.json
new file mode 100644
index 0000000..66769ed
--- /dev/null
+++ b/uimaj-json/src/test/resources/Json2Deserializer/feature_structures_only.json
@@ -0,0 +1,23 @@
+[ {
+ "%ID" : "0",
+ "%TYPE" : "Sofa",
+ "sofaNum" : 1,
+ "sofaID" : "_InitialView",
+ "mimeType" : "text",
+ "sofaString" : "Hello world"
+}, {
+ "%ID" : "1",
+ "%TYPE" : "Annotation",
+ "%VIEWS" : [ "_InitialView" ],
+ "@sofa" : "0",
+ "begin" : 0,
+ "end" : 10
+}, {
+ "%ID" : "2",
+ "%TYPE" : "DocumentAnnotation",
+ "%VIEWS" : [ "_InitialView" ],
+ "@sofa" : "0",
+ "begin" : 0,
+ "end" : 11,
+ "language" : "en"
+} ]
diff --git a/uimaj-json/src/test/resources/Json2Deserializer/text_only.json b/uimaj-json/src/test/resources/Json2Deserializer/text_only.json
new file mode 100644
index 0000000..6bdab32
--- /dev/null
+++ b/uimaj-json/src/test/resources/Json2Deserializer/text_only.json
@@ -0,0 +1 @@
+"Hello world."
\ No newline at end of file
diff --git a/uimaj-json/src/test/resources/Json2SerializerTest/customAnnotationType/reference.json b/uimaj-json/src/test/resources/Json2SerializerTest/customAnnotationType/reference.json
new file mode 100644
index 0000000..dd44fc2
--- /dev/null
+++ b/uimaj-json/src/test/resources/Json2SerializerTest/customAnnotationType/reference.json
@@ -0,0 +1,21 @@
+{
+ "%TYPES" : {
+ "custom.Annotation" : {
+ "%SUPER_TYPE" : "uima.tcas.Annotation",
+ "value" : "uima.cas.String"
+ }
+ },
+ "%FEATURE_STRUCTURES" : [ {
+ "%ID" : "0",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 1,
+ "sofaID" : "_InitialView"
+ }, {
+ "%ID" : "1",
+ "%TYPE" : "custom.Annotation",
+ "%VIEWS" : [ "_InitialView" ],
+ "@sofa" : "0",
+ "begin" : 0,
+ "end" : 10
+ } ]
+}
\ No newline at end of file
diff --git a/uimaj-json/src/test/resources/Json2SerializerTest/featureStructureIndexedInMultipleViewsInline/reference.json b/uimaj-json/src/test/resources/Json2SerializerTest/featureStructureIndexedInMultipleViewsInline/reference.json
new file mode 100644
index 0000000..571f94b
--- /dev/null
+++ b/uimaj-json/src/test/resources/Json2SerializerTest/featureStructureIndexedInMultipleViewsInline/reference.json
@@ -0,0 +1,39 @@
+{
+ "%FEATURE_STRUCTURES" : [ {
+ "%ID" : "0",
+ "%TYPE" : "uima.cas.TOP",
+ "%VIEWS" : [ "_InitialView", "secondView" ]
+ }, {
+ "%ID" : "1",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 1,
+ "sofaID" : "_InitialView",
+ "mimeType" : "text",
+ "sofaString" : "First view"
+ }, {
+ "%ID" : "2",
+ "%TYPE" : "uima.tcas.DocumentAnnotation",
+ "%VIEWS" : [ "_InitialView" ],
+ "%FLAGS" : [ "DocumentAnnotation" ],
+ "@sofa" : "1",
+ "begin" : 0,
+ "end" : 10,
+ "language" : "x-unspecified"
+ }, {
+ "%ID" : "3",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 2,
+ "sofaID" : "secondView",
+ "mimeType" : "text",
+ "sofaString" : "Second view"
+ }, {
+ "%ID" : "4",
+ "%TYPE" : "uima.tcas.DocumentAnnotation",
+ "%VIEWS" : [ "secondView" ],
+ "%FLAGS" : [ "DocumentAnnotation" ],
+ "@sofa" : "3",
+ "begin" : 0,
+ "end" : 11,
+ "language" : "x-unspecified"
+ } ]
+}
\ No newline at end of file
diff --git a/uimaj-json/src/test/resources/Json2SerializerTest/featureStructureIndexedInMultipleViewsSeparate/reference.json b/uimaj-json/src/test/resources/Json2SerializerTest/featureStructureIndexedInMultipleViewsSeparate/reference.json
new file mode 100644
index 0000000..e3d7825
--- /dev/null
+++ b/uimaj-json/src/test/resources/Json2SerializerTest/featureStructureIndexedInMultipleViewsSeparate/reference.json
@@ -0,0 +1,40 @@
+{
+ "%VIEWS" : {
+ "_InitialView" : [ "0", "1", "2" ],
+ "secondView" : [ "3", "4", "2" ]
+ },
+ "%FEATURE_STRUCTURES" : [ {
+ "%ID" : "2",
+ "%TYPE" : "uima.cas.TOP"
+ }, {
+ "%ID" : "0",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 1,
+ "sofaID" : "_InitialView",
+ "mimeType" : "text",
+ "sofaString" : "First view"
+ }, {
+ "%ID" : "1",
+ "%TYPE" : "uima.tcas.DocumentAnnotation",
+ "%FLAGS" : [ "DocumentAnnotation" ],
+ "@sofa" : "0",
+ "begin" : 0,
+ "end" : 10,
+ "language" : "x-unspecified"
+ }, {
+ "%ID" : "3",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 2,
+ "sofaID" : "secondView",
+ "mimeType" : "text",
+ "sofaString" : "Second view"
+ }, {
+ "%ID" : "4",
+ "%TYPE" : "uima.tcas.DocumentAnnotation",
+ "%FLAGS" : [ "DocumentAnnotation" ],
+ "@sofa" : "3",
+ "begin" : 0,
+ "end" : 11,
+ "language" : "x-unspecified"
+ } ]
+}
\ No newline at end of file
diff --git a/uimaj-json/src/test/resources/Json2SerializerTest/multipleViewsAndSofas/reference.json b/uimaj-json/src/test/resources/Json2SerializerTest/multipleViewsAndSofas/reference.json
new file mode 100644
index 0000000..33c5fea
--- /dev/null
+++ b/uimaj-json/src/test/resources/Json2SerializerTest/multipleViewsAndSofas/reference.json
@@ -0,0 +1,35 @@
+{
+ "%FEATURE_STRUCTURES" : [ {
+ "%ID" : "0",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 1,
+ "sofaID" : "_InitialView",
+ "mimeType" : "text",
+ "sofaString" : "First view"
+ }, {
+ "%ID" : "1",
+ "%TYPE" : "uima.tcas.DocumentAnnotation",
+ "%VIEWS" : [ "_InitialView" ],
+ "%FLAGS" : [ "DocumentAnnotation" ],
+ "@sofa" : "0",
+ "begin" : 0,
+ "end" : 10,
+ "language" : "x-unspecified"
+ }, {
+ "%ID" : "2",
+ "%TYPE" : "uima.cas.Sofa",
+ "sofaNum" : 2,
+ "sofaID" : "secondView",
+ "mimeType" : "text",
+ "sofaString" : "Second view"
+ }, {
+ "%ID" : "3",
+ "%TYPE" : "uima.tcas.DocumentAnnotation",
+ "%VIEWS" : [ "secondView" ],
+ "%FLAGS" : [ "DocumentAnnotation" ],
+ "@sofa" : "2",
+ "begin" : 0,
+ "end" : 11,
+ "language" : "x-unspecified"
+ } ]
+}
\ No newline at end of file