@Override public boolean asBoolean() { return value==Iv.N ? super.asBoolean() : value==Iv.T; }
public String stringify(JsonValue value) { if (!value.isNumber()) return null; double val=value.asDouble(); if (val==Double.POSITIVE_INFINITY) return "Inf"; else if (val==Double.NEGATIVE_INFINITY) return "-Inf"; else if (Double.isNaN(val)) return "NaN"; else if (val==0.0 && 1/val==Double.NEGATIVE_INFINITY) return "-0"; else return null; } }
public void save(JsonValue value, Writer tw, int level) throws IOException { boolean following=false; switch (value.getType()) { case OBJECT: JsonObject obj=value.asObject(); if (obj.size()>0) nl(tw, level); tw.write('{'); JsonType vType=v.getType(); if (format && vType!=JsonType.ARRAY && vType!=JsonType.OBJECT) tw.write(" "); if (v==null) tw.write("null"); break; case ARRAY: JsonArray arr=value.asArray(); int n=arr.size(); if (n>0) nl(tw, level); if (following) tw.write(","); JsonValue v=arr.get(i); JsonType vType=v.getType(); if (vType!=JsonType.ARRAY && vType!=JsonType.OBJECT) nl(tw, level+1); save(v, tw, level+1); break; case BOOLEAN: tw.write(value.isTrue()?"true":"false"); break; case STRING: tw.write('"');
/** * Returns the JSON string for this value in its minimal form, without any additional whitespace. * The result is guaranteed to be a valid input for the method {@link #readJSON(String)} and to * create a value that is <em>equal</em> to this object. * * @return a JSON string that represents this value */ @Override public String toString() { return toString(Stringify.PLAIN); }
Object convertJsonToJava(final JsonValue value) throws PersistentStoreException { if (null == value) { return null; } else if (value.isString()) { return value.asString(); } else if (value.isNumber()) { return value.asDouble(); } else if (value.isBoolean()) { return value.asBoolean(); } else if (value.isArray()) { final List<Object> list = new ArrayList<>(); for (final JsonValue av : value.asArray()) { final Object o = this.convertJsonToJava(av); list.add(o); } return list; } else if (value.isObject()) { final Map<String, Object> map = new HashMap<>(); for (final String k : value.asObject().names()) { final JsonValue jv = value.asObject().get(k); final Object v = this.convertJsonToJava(jv); map.put(k, v); } return map; } else { throw new PersistentStoreException("Unknown JSON type.", null); } }
private static boolean test(String name, String file, boolean inputCr, boolean outputCr) throws Exception { int extIdx=file.lastIndexOf('.'); boolean isJson=extIdx>=0 && file.substring(extIdx).equals(".json"); boolean shouldFail=name.startsWith("fail"); JsonValue.setEol(outputCr?"\r\n":"\n"); String text=load(file, inputCr); try { HjsonOptions opt=new HjsonOptions(); opt.setParseLegacyRoot(false); JsonValue data=JsonValue.readHjson(text, opt); String data1=data.toString(Stringify.FORMATTED); String hjson1=data.toString(Stringify.HJSON); if (!shouldFail) { JsonValue result=JsonValue.readJSON(load(name+"_result.json", inputCr)); String data2=result.toString(Stringify.FORMATTED); String hjson2=load(name+"_result.hjson", outputCr); if (!data1.equals(data2)) return failErr(name, "parse", data1, data2); if (!hjson1.equals(hjson2)) return failErr(name, "stringify", hjson1, hjson2); if (isJson) { String json1=data.toString(), json2=JsonValue.readHjson(text, opt).toString(); if (!json1.equals(json2)) return failErr(name, "json chk", json1, json2); } } else return failErr(name, "should fail", null, null); } catch (Exception e) { if (!shouldFail) return failErr(name, "exception", e.toString(), ""); } return true; }
@Override public void toJson(final Writer out, final T object) { try (StringWriter writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); JsonValue.readHjson(writer.toString()).writeTo(out, Stringify.FORMATTED); } catch (final Exception e) { throw new IllegalArgumentException(e); } }
@Override public WildcardCondition create(String condition) { boolean renamed = false; if (condition.startsWith(NOT_TOKEN)) { condition = StringUtils.replaceFirst(condition, NOT_TOKEN, TOKEN); renamed = true; } String[] elements = StringUtils.splitFirst(condition, " "); boolean not = renamed; if (elements.length < 2) { throw new PandaFrameworkException("Token condition does renamed contain specification"); } String source = elements[1] .replace("{", "{" + System.lineSeparator()) .replace("}", System.lineSeparator() + "}"); JsonObject conditions = JsonValue .readHjson(source) .asObject(); return representation -> check(conditions, representation).negate(not); }
JsonValue getJson() throws IOException, FilesystemException { if (null == this.json_cache) { final IFile file = this.getFile(); if (file.exists()) { this.json_cache = JsonValue.readHjson(file.reader()); } else { } } return this.json_cache; }
/** * Returns the <code>String</code> value of the member with the specified name in this object. If * this object does not contain a member with this name, the given default value is returned. If * this object contains multiple members with the given name, the last one is picked. If this * member's value does not represent a JSON string, an exception is thrown. * * @param name * the name of the member whose value is to be returned * @param defaultValue * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name */ public String getString(String name, String defaultValue) { JsonValue value=get(name); return value!=null ? value.asString() : defaultValue; }
/** * Writes the JSON representation of this value to the given writer in its minimal form, without * any additional whitespace. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer) throws IOException { writeTo(writer, Stringify.PLAIN); }
public void save(JsonValue value, Writer tw, int level) throws IOException { boolean following=false; switch (value.getType()) { case OBJECT: JsonObject obj=value.asObject(); if (obj.size()>0) nl(tw, level); tw.write('{'); JsonType vType=v.getType(); if (format && vType!=JsonType.ARRAY && vType!=JsonType.OBJECT) tw.write(" "); if (v==null) tw.write("null"); break; case ARRAY: JsonArray arr=value.asArray(); int n=arr.size(); if (n>0) nl(tw, level); if (following) tw.write(","); JsonValue v=arr.get(i); JsonType vType=v.getType(); if (vType!=JsonType.ARRAY && vType!=JsonType.OBJECT) nl(tw, level+1); save(v, tw, level+1); break; case BOOLEAN: tw.write(value.isTrue()?"true":"false"); break; case STRING: tw.write('"');
/** * Returns the JSON string for this value in its minimal form, without any additional whitespace. * The result is guaranteed to be a valid input for the method {@link #readJSON(String)} and to * create a value that is <em>equal</em> to this object. * * @return a JSON string that represents this value */ @Override public String toString() { return toString(Stringify.PLAIN); }
if (null == value) { return null; } else if (value.isString()) { final T t = this.af.createDatatype(itemType, value.asString()); return t; } else if (value.isNumber()) { final T t = this.af.createDatatype(itemType, value.asDouble()); return t; } else if (value.isBoolean()) { final T t = this.af.createDatatype(itemType, value.asBoolean()); return t; } else if (value.isArray()) { if (List.class.isAssignableFrom(itemType)) { final List list = new ArrayList<>(); for (final JsonValue av : value.asArray()) { final Object o = this.convertJsonToJava(av); list.add(o); throw new PersistentStoreException("Cannot convert JSON Array to List.", null); } else if (value.isObject()) { if (Map.class.isAssignableFrom(itemType)) { final Map<String, Object> map = new HashMap<>(); for (final String k : value.asObject().names()) { final JsonValue jv = value.asObject().get(k); final Object v = this.convertJsonToJava(jv); map.put(k, v);
@Override public void toJson(final File out, final T object) { try (StringWriter writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); JsonValue.readHjson(writer.toString()).writeTo(new BufferedWriter(new FileWriter(out))); } catch (final Exception e) { throw new IllegalArgumentException(e); } }