allow duplicates in json parser

This commit is contained in:
Grahame Grieve 2019-04-07 21:09:49 +10:00
parent 62df09f9a9
commit 07931c2ced
2 changed files with 49 additions and 14 deletions

View File

@ -336,6 +336,7 @@ public class JsonTrackingParser {
private ItemType itemType = ItemType.Object;
private String itemName;
private String itemValue;
private boolean errorOnDuplicates = true;
public static JsonObject parseJson(String source) throws IOException {
return parse(source, null);
@ -349,6 +350,10 @@ public class JsonTrackingParser {
return parse(TextFile.bytesToString(stream), null);
}
public static JsonObject parseJson(byte[] stream, boolean allowDuplicates) throws IOException {
return parse(TextFile.bytesToString(stream), null, allowDuplicates);
}
public static JsonObject parseJson(File source) throws IOException {
return parse(TextFile.fileToString(source), null);
}
@ -357,9 +362,14 @@ public class JsonTrackingParser {
return parse(TextFile.fileToString(source), null);
}
public static JsonObject parse(String source, Map<JsonElement, LocationData> map) throws IOException {
public static JsonObject parse(String source, Map<JsonElement, LocationData> map) throws IOException {
return parse(source, map, false);
}
public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
JsonTrackingParser self = new JsonTrackingParser();
self.map = map;
self.setErrorOnDuplicates(!allowDuplicates);
return self.parse(source);
}
@ -386,14 +396,14 @@ public class JsonTrackingParser {
map.put(obj, lexer.location.copy());
while (!(itemType == ItemType.End) || (root && (itemType == ItemType.Eof))) {
if (obj.has(itemName))
throw lexer.error("Duplicated property name: "+itemName);
switch (itemType) {
case Object:
JsonObject child = new JsonObject(); //(obj.path+'.'+ItemName);
LocationData loc = lexer.location.copy();
obj.add(itemName, child);
if (!obj.has(itemName))
obj.add(itemName, child);
else if (errorOnDuplicates)
throw lexer.error("Duplicated property name: "+itemName);
next();
readObject(child, false);
if (map != null)
@ -401,32 +411,47 @@ public class JsonTrackingParser {
break;
case Boolean :
JsonPrimitive v = new JsonPrimitive(Boolean.valueOf(itemValue));
obj.add(itemName, v);
if (!obj.has(itemName))
obj.add(itemName, v);
else if (errorOnDuplicates)
throw lexer.error("Duplicated property name: "+itemName);
if (map != null)
map.put(v, lexer.location.copy());
break;
case String:
v = new JsonPrimitive(itemValue);
obj.add(itemName, v);
if (!obj.has(itemName))
obj.add(itemName, v);
else if (errorOnDuplicates)
throw lexer.error("Duplicated property name: "+itemName);
if (map != null)
map.put(v, lexer.location.copy());
break;
case Number:
v = new JsonPrimitive(new PresentedBigDecimal(itemValue));
obj.add(itemName, v);
if (!obj.has(itemName))
obj.add(itemName, v);
else if (errorOnDuplicates)
throw lexer.error("Duplicated property name: "+itemName);
if (map != null)
map.put(v, lexer.location.copy());
break;
case Null:
JsonNull n = new JsonNull();
obj.add(itemName, n);
if (!obj.has(itemName))
obj.add(itemName, n);
else if (errorOnDuplicates)
throw lexer.error("Duplicated property name: "+itemName);
if (map != null)
map.put(n, lexer.location.copy());
break;
case Array:
JsonArray arr = new JsonArray(); // (obj.path+'.'+ItemName);
loc = lexer.location.copy();
obj.add(itemName, arr);
if (!obj.has(itemName))
obj.add(itemName, arr);
else if (errorOnDuplicates)
throw lexer.error("Duplicated property name: "+itemName);
next();
if (!readArray(arr, false))
next(true);
@ -585,4 +610,14 @@ public class JsonTrackingParser {
throw lexer.error("not done yet (b): "+lexer.getType().toString());
}
}
public boolean isErrorOnDuplicates() {
return errorOnDuplicates;
}
public void setErrorOnDuplicates(boolean errorOnDuplicates) {
this.errorOnDuplicates = errorOnDuplicates;
}
}

View File

@ -4,11 +4,11 @@ REM make sure you are committed
echo
echo ===============================================================
echo upgrade and release fhir.core from 3.7.21-SNAPSHOT to 3.7.22-SNAPSHOT
echo upgrade and release fhir.core from 3.7.22-SNAPSHOT to 3.7.23-SNAPSHOT
echo ===============================================================
pause
call mvn versions:set -DnewVersion=3.7.22-SNAPSHOT
call mvn versions:set -DnewVersion=3.7.23-SNAPSHOT
echo ===============================================================
echo upgraded version number using maven
@ -25,8 +25,8 @@ echo next: replace references in java code + ivy
echo ===============================================================
pause
call "C:\tools\fnr.exe" --cl --dir "C:\work\org.hl7.fhir\build" --fileMask "*.java" --excludeFileMask "*.dll, *.exe" --includeSubDirectories --find "3.7.21-SNAPSHOT" --replace "3.7.22-SNAPSHOT"
call "C:\tools\fnr.exe" --cl --dir "C:\work\org.hl7.fhir\build" --fileMask "*.xml" --excludeFileMask "*.dll, *.exe" --find "3.7.21-SNAPSHOT" --replace "3.7.22-SNAPSHOT"
call "C:\tools\fnr.exe" --cl --dir "C:\work\org.hl7.fhir\build" --fileMask "*.java" --excludeFileMask "*.dll, *.exe" --includeSubDirectories --find "3.7.22-SNAPSHOT" --replace "3.7.23-SNAPSHOT"
call "C:\tools\fnr.exe" --cl --dir "C:\work\org.hl7.fhir\build" --fileMask "*.xml" --excludeFileMask "*.dll, *.exe" --find "3.7.22-SNAPSHOT" --replace "3.7.23-SNAPSHOT"
echo ===============================================================
echo done replacing references