add json5 mapper
This commit is contained in:
parent
27931daca6
commit
38e451b5fa
@ -0,0 +1,53 @@
|
|||||||
|
package de.marhali.easyi18n.io.parser.json5;
|
||||||
|
|
||||||
|
import de.marhali.easyi18n.io.parser.ArrayMapper;
|
||||||
|
import de.marhali.easyi18n.util.StringUtil;
|
||||||
|
import de.marhali.json5.Json5;
|
||||||
|
import de.marhali.json5.Json5Array;
|
||||||
|
import de.marhali.json5.Json5Primitive;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map json5 array values.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
|
public class Json5ArrayMapper extends ArrayMapper {
|
||||||
|
|
||||||
|
private static final Json5 JSON5 = Json5.builder(builder ->
|
||||||
|
builder.allowInvalidSurrogate().quoteSingle().indentFactor(0).build());
|
||||||
|
|
||||||
|
public static String read(Json5Array array) {
|
||||||
|
return read(array.iterator(), (jsonElement -> {
|
||||||
|
try {
|
||||||
|
return jsonElement.isJsonArray() || jsonElement.isJsonObject()
|
||||||
|
? "\\" + JSON5.serialize(jsonElement)
|
||||||
|
: jsonElement.getAsString();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AssertionError(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Json5Array write(String concat) {
|
||||||
|
Json5Array array = new Json5Array();
|
||||||
|
|
||||||
|
write(concat, (element) -> {
|
||||||
|
if(element.startsWith("\\")) {
|
||||||
|
array.add(JSON5.parse(element.replace("\\", "")));
|
||||||
|
} else {
|
||||||
|
if(StringUtil.isHexString(element)) {
|
||||||
|
array.add(Json5Primitive.of(element, true));
|
||||||
|
} else if(NumberUtils.isNumber(element)) {
|
||||||
|
array.add(Json5Primitive.of(NumberUtils.createNumber(element)));
|
||||||
|
} else {
|
||||||
|
array.add(Json5Primitive.of(element));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
package de.marhali.easyi18n.io.parser.json5;
|
||||||
|
|
||||||
|
import de.marhali.easyi18n.model.Translation;
|
||||||
|
import de.marhali.easyi18n.model.TranslationNode;
|
||||||
|
import de.marhali.easyi18n.util.StringUtil;
|
||||||
|
|
||||||
|
import de.marhali.json5.Json5Element;
|
||||||
|
import de.marhali.json5.Json5Object;
|
||||||
|
import de.marhali.json5.Json5Primitive;
|
||||||
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapper for mapping json5 objects into translation nodes and backwards.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
|
public class Json5Mapper {
|
||||||
|
public static void read(String locale, Json5Object json, TranslationNode node) {
|
||||||
|
for(Map.Entry<String, Json5Element> entry : json.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
Json5Element value = entry.getValue();
|
||||||
|
|
||||||
|
TranslationNode childNode = node.getOrCreateChildren(key);
|
||||||
|
|
||||||
|
if(value.isJsonObject()) {
|
||||||
|
// Nested element - run recursively
|
||||||
|
read(locale, value.getAsJsonObject(), childNode);
|
||||||
|
} else {
|
||||||
|
Translation translation = childNode.getValue();
|
||||||
|
|
||||||
|
String content = value.isJsonArray()
|
||||||
|
? Json5ArrayMapper.read(value.getAsJsonArray())
|
||||||
|
: StringUtil.escapeControls(value.getAsString(), true);
|
||||||
|
|
||||||
|
translation.put(locale, content);
|
||||||
|
childNode.setValue(translation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void write(String locale, Json5Object json, TranslationNode node) {
|
||||||
|
for(Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
TranslationNode childNode = entry.getValue();
|
||||||
|
|
||||||
|
if(!childNode.isLeaf()) {
|
||||||
|
// Nested node - run recursively
|
||||||
|
Json5Object childJson = new Json5Object();
|
||||||
|
write(locale, childJson, childNode);
|
||||||
|
if(childJson.size() > 0) {
|
||||||
|
json.add(key, childJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Translation translation = childNode.getValue();
|
||||||
|
String content = translation.get(locale);
|
||||||
|
if(content != null) {
|
||||||
|
if(Json5ArrayMapper.isArray(content)) {
|
||||||
|
json.add(key, Json5ArrayMapper.write(content));
|
||||||
|
} else if(StringUtil.isHexString(content)) {
|
||||||
|
json.add(key, Json5Primitive.of(content, true));
|
||||||
|
} else if(NumberUtils.isNumber(content)) {
|
||||||
|
json.add(key, Json5Primitive.of(NumberUtils.createNumber(content)));
|
||||||
|
} else {
|
||||||
|
json.add(key, Json5Primitive.of(StringEscapeUtils.unescapeJava(content)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user