support comments in json source when configured to do so
This commit is contained in:
parent
9e59ce2323
commit
1da57be1c0
|
@ -73,6 +73,11 @@ public class JsonParser extends JsonParserBase {
|
|||
setAllowUnknownContent(allowUnknownContent);
|
||||
}
|
||||
|
||||
public JsonParser(boolean allowUnknownContent, boolean allowComments) {
|
||||
super();
|
||||
setAllowUnknownContent(allowUnknownContent);
|
||||
setAllowComments(allowComments);
|
||||
}
|
||||
|
||||
protected void parseElementProperties(JsonObject json, Element element) throws IOException, FHIRFormatError {
|
||||
super.parseElementProperties(json, element);
|
||||
|
|
|
@ -198,7 +198,7 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
|
|||
private boolean htmlPretty;
|
||||
|
||||
private JsonObject loadJson(InputStream input) throws JsonSyntaxException, IOException {
|
||||
return JsonTrackingParser.parse(TextFile.streamToString(input), null);
|
||||
return JsonTrackingParser.parse(TextFile.streamToString(input), null, allowUnknownContent, allowComments);
|
||||
// return parser.parse(TextFile.streamToString(input)).getAsJsonObject();
|
||||
}
|
||||
|
||||
|
|
|
@ -145,6 +145,11 @@ public abstract class ParserBase extends FormatUtilities implements IParser {
|
|||
*/
|
||||
protected boolean allowUnknownContent;
|
||||
|
||||
/**
|
||||
* whether to allow comments in the json (special case for IG publisher source)
|
||||
*/
|
||||
protected boolean allowComments;
|
||||
|
||||
/**
|
||||
* @return Whether to throw an exception if unknown content is found (or just skip it)
|
||||
*/
|
||||
|
@ -158,7 +163,15 @@ public abstract class ParserBase extends FormatUtilities implements IParser {
|
|||
this.allowUnknownContent = allowUnknownContent;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public boolean isAllowComments() {
|
||||
return allowComments;
|
||||
}
|
||||
|
||||
public void setAllowComments(boolean allowComments) {
|
||||
this.allowComments = allowComments;
|
||||
}
|
||||
|
||||
protected OutputStyle style = OutputStyle.NORMAL;
|
||||
|
||||
public OutputStyle getOutputStyle() {
|
||||
|
|
|
@ -63,6 +63,7 @@ public class JsonParser extends ParserBase {
|
|||
|
||||
private JsonCreator json;
|
||||
private Map<JsonElement, LocationData> map;
|
||||
private boolean allowComments;
|
||||
|
||||
public JsonParser(IWorkerContext context) {
|
||||
super(context);
|
||||
|
@ -92,7 +93,7 @@ public class JsonParser extends ParserBase {
|
|||
if (policy == ValidationPolicy.EVERYTHING) {
|
||||
JsonObject obj = null;
|
||||
try {
|
||||
obj = JsonTrackingParser.parse(source, map);
|
||||
obj = JsonTrackingParser.parse(source, map, false, allowComments);
|
||||
} catch (Exception e) {
|
||||
logError(-1, -1,context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
|
||||
return null;
|
||||
|
@ -535,4 +536,14 @@ public class JsonParser extends ParserBase {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isAllowComments() {
|
||||
return allowComments;
|
||||
}
|
||||
|
||||
public JsonParser setAllowComments(boolean allowComments) {
|
||||
this.allowComments = allowComments;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -76,6 +76,12 @@ public class JsonParser extends JsonParserBase {
|
|||
setAllowUnknownContent(allowUnknownContent);
|
||||
}
|
||||
|
||||
public JsonParser(boolean allowUnknownContent, boolean allowComments) {
|
||||
super();
|
||||
setAllowUnknownContent(allowUnknownContent);
|
||||
setAllowComments(allowComments);
|
||||
}
|
||||
|
||||
protected void parseBaseProperties(JsonObject json, Base res) throws IOException, FHIRFormatError {
|
||||
// nothing
|
||||
}
|
||||
|
|
|
@ -196,7 +196,7 @@ public abstract class JsonParserBase extends ParserBase implements IParser {
|
|||
private boolean htmlPretty;
|
||||
|
||||
private JsonObject loadJson(InputStream input) throws JsonSyntaxException, IOException {
|
||||
return JsonTrackingParser.parse(TextFile.streamToString(input), null);
|
||||
return JsonTrackingParser.parse(TextFile.streamToString(input), null, allowUnknownContent, allowComments);
|
||||
// return parser.parse(TextFile.streamToString(input)).getAsJsonObject();
|
||||
}
|
||||
|
||||
|
|
|
@ -145,6 +145,11 @@ public abstract class ParserBase extends FormatUtilities implements IParser {
|
|||
*/
|
||||
protected boolean allowUnknownContent;
|
||||
|
||||
/**
|
||||
* whether to allow comments in the json (special case for IG publisher source)
|
||||
*/
|
||||
protected boolean allowComments;
|
||||
|
||||
/**
|
||||
* @return Whether to throw an exception if unknown content is found (or just skip it)
|
||||
*/
|
||||
|
@ -159,6 +164,14 @@ public abstract class ParserBase extends FormatUtilities implements IParser {
|
|||
return this;
|
||||
}
|
||||
|
||||
public boolean isAllowComments() {
|
||||
return allowComments;
|
||||
}
|
||||
|
||||
public void setAllowComments(boolean allowComments) {
|
||||
this.allowComments = allowComments;
|
||||
}
|
||||
|
||||
protected OutputStyle style = OutputStyle.NORMAL;
|
||||
|
||||
public OutputStyle getOutputStyle() {
|
||||
|
|
|
@ -62,7 +62,6 @@ public class JsonTrackingParser {
|
|||
return presentation;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public enum TokenType {
|
||||
|
@ -243,6 +242,16 @@ public class JsonTrackingParser {
|
|||
char ch;
|
||||
do {
|
||||
ch = getNextChar();
|
||||
if (allowComments && ch == '/') {
|
||||
char ch1 = getNextChar();
|
||||
if (ch1 == '/') {
|
||||
while (more() && !Utilities.charInSet(ch, '\r', '\n')) {
|
||||
ch = getNextChar();
|
||||
}
|
||||
} else {
|
||||
push(ch1);
|
||||
}
|
||||
}
|
||||
} while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
|
||||
lastLocationAWS = location.copy();
|
||||
|
||||
|
@ -322,7 +331,7 @@ public class JsonTrackingParser {
|
|||
|
||||
public String consume(TokenType type) throws IOException {
|
||||
if (this.type != type)
|
||||
throw error("JSON syntax error - found "+type.toString()+" expecting "+type.toString());
|
||||
throw error("JSON syntax error - found "+this.type.toString()+" expecting "+type.toString());
|
||||
String result = value;
|
||||
next();
|
||||
return result;
|
||||
|
@ -339,6 +348,7 @@ public class JsonTrackingParser {
|
|||
private String itemName;
|
||||
private String itemValue;
|
||||
private boolean errorOnDuplicates = true;
|
||||
private boolean allowComments = false;
|
||||
|
||||
public static JsonObject parseJson(String source) throws IOException {
|
||||
return parse(source, null);
|
||||
|
@ -369,9 +379,14 @@ public class JsonTrackingParser {
|
|||
}
|
||||
|
||||
public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
|
||||
return parse(source, map, allowDuplicates, false);
|
||||
}
|
||||
|
||||
public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates, boolean allowComments) throws IOException {
|
||||
JsonTrackingParser self = new JsonTrackingParser();
|
||||
self.map = map;
|
||||
self.setErrorOnDuplicates(!allowDuplicates);
|
||||
self.setAllowComments(allowComments);
|
||||
return self.parse(Utilities.stripBOM(source));
|
||||
}
|
||||
|
||||
|
@ -621,6 +636,15 @@ public class JsonTrackingParser {
|
|||
this.errorOnDuplicates = errorOnDuplicates;
|
||||
}
|
||||
|
||||
|
||||
public boolean isAllowComments() {
|
||||
return allowComments;
|
||||
}
|
||||
|
||||
public void setAllowComments(boolean allowComments) {
|
||||
this.allowComments = allowComments;
|
||||
}
|
||||
|
||||
public static void write(JsonObject json, File file) throws IOException {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String jcnt = gson.toJson(json);
|
||||
|
|
Loading…
Reference in New Issue