Skip to content

Commit

Permalink
#513: Added a JXSON format option to support using the Jackson serial…
Browse files Browse the repository at this point in the history
…izer to produce JSON output.
  • Loading branch information
brynrhodes committed Apr 21, 2020
1 parent aebe22c commit efcdc64
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Src/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ configure(subprojects.findAll {it.name in ['model', 'elm', 'quick', 'qdm']}) {
runtime group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.4.0-b180830.0438'
runtime group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
runtime group: 'com.sun.activation', name: 'javax.activation', version: '1.2.0'
runtime group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.1'
runtime group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.10.1'
runtime group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.1'
runtime group: 'com.fasterxml.jackson.module', name: 'jackson-module-jaxb-annotations', version: '2.10.1'
}

ext.xjc = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.cqframework.cql.cql2elm;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ParseTree;
import org.cqframework.cql.cql2elm.model.TranslatedLibrary;
import org.cqframework.cql.cql2elm.model.serialization.LibraryWrapper;
import org.cqframework.cql.cql2elm.preprocessor.CqlPreprocessorVisitor;
import org.cqframework.cql.elm.tracking.TrackBack;
import org.cqframework.cql.gen.cqlLexer;
Expand Down Expand Up @@ -44,7 +50,7 @@ public static enum Options {
DisableMethodInvocation,
RequireFromKeyword
}
public static enum Format { XML, JSON, COFFEE }
public static enum Format { XML, JSON, JXSON, COFFEE }
private static JAXBContext jaxbContext;

private Library library = null;
Expand Down Expand Up @@ -325,10 +331,23 @@ private String toJson(Library library) {
return convertToJson(library);
}
catch (JAXBException e) {
throw new IllegalArgumentException("Could not convert library to JSON.", e);
throw new IllegalArgumentException("Could not convert library to JSON using JAXB serializer.", e);
}
}

private String toJxson(Library library) {
try {
return convertToJxson(library);
}
catch (JsonProcessingException e) {
throw new IllegalArgumentException("Could not convert library to JSON using Jackson serializer.", e);
}
}

public String toJxson() {
return toJxson(library);
}

public String toJson() {
return toJson(library);
}
Expand Down Expand Up @@ -377,6 +396,14 @@ public Map<String, String> getLibrariesAsJSON() {
return result;
}

public Map<String, String> getLibrariesAsJXSON() {
Map<String, String> result = new HashMap();
for (Map.Entry<String, TranslatedLibrary> entry : libraryManager.getTranslatedLibraries().entrySet()) {
result.put(entry.getKey(), toJxson(entry.getValue().getLibrary()));
}
return result;
}

public List<CqlTranslatorException> getExceptions() { return exceptions; }

public List<CqlTranslatorException> getErrors() { return errors; }
Expand Down Expand Up @@ -410,7 +437,6 @@ public CqlErrorListener(LibraryBuilder builder, boolean detailedErrors) {
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, RecognitionException e) {
TrackBack trackback = new TrackBack(new VersionedIdentifier().withId("unknown"), line, charPositionInLine, line, charPositionInLine);
// CqlTranslator.this.errors.add(new CqlTranslatorException(msg, trackback, e));

if (detailedErrors) {
builder.recordParsingException(new CqlSyntaxException(msg, trackback, e));
Expand Down Expand Up @@ -519,6 +545,17 @@ public String convertToJson(Library library) throws JAXBException {
return writer.getBuffer().toString();
}

public String convertToJxson(Library library) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
JaxbAnnotationModule annotationModule = new JaxbAnnotationModule();
mapper.registerModule(annotationModule);
LibraryWrapper wrapper = new LibraryWrapper();
wrapper.setLibrary(library);
return mapper.writeValueAsString(wrapper);
}

public static void loadModelInfo(File modelInfoXML) {
final ModelInfo modelInfo = JAXB.unmarshal(modelInfoXML, ModelInfo.class);
final VersionedIdentifier modelId = new VersionedIdentifier().withId(modelInfo.getName()).withVersion(modelInfo.getVersion());
Expand Down Expand Up @@ -616,6 +653,9 @@ private static void writeELM(Path inPath, Path outPath, CqlTranslator.Format for
pw.print("module.exports = ");
pw.println(translator.toJson());
break;
case JXSON:
pw.println(translator.toJxson());
break;
case JSON:
pw.println(translator.toJson());
break;
Expand Down Expand Up @@ -700,6 +740,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
}
switch (outputFormat) {
case JSON:
case JXSON:
name += ".json";
break;
case COFFEE:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.cqframework.cql.cql2elm.model.serialization;

import org.hl7.elm.r1.Library;

public class LibraryWrapper {
private Library library;
public Library getLibrary() {
return this.library;
}

public void setLibrary(Library library) {
this.library = library;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.cqframework.cql.elm.tracking;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.hl7.cql.model.DataType;

import javax.xml.bind.annotation.XmlTransient;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public class Trackable {
private final UUID trackerId;
private final List<TrackBack> trackbacks;
Expand All @@ -18,15 +22,20 @@ public Trackable() {
this.trackbacks = new ArrayList<>();
}

@XmlTransient
@JsonIgnore
public UUID getTrackerId() {
return trackerId;
}

@XmlTransient
@JsonIgnore
public List<TrackBack> getTrackbacks() {
return trackbacks;
}

@XmlTransient
@JsonIgnore
public DataType getResultType() {
return resultType;
}
Expand Down

0 comments on commit efcdc64

Please sign in to comment.