@SuppressWarnings("unchecked") @Override public JsonPrimitive deepClone() { // all supported value types are immutable so no need to clone those. return new JsonPrimitive(value,type); }
private static void serialize(BsonGenerator generator, JsonPrimitive p) throws JsonGenerationException, IOException { if(p.isBoolean()) { generator.writeBoolean(p.asBoolean()); } else if(p.isNumber()) { if(p.asString().contains(".")) { generator.writeNumber(p.asDouble()); } else { generator.writeNumber(p.asLong()); } } else if(p.isString()) { generator.writeString(p.asString()); } else { generator.writeNull(); } } }
@Override public void formatTo(Formatter formatter, int flags, int width, int precision) { // TODO actually support more stuff here for int/double formatting // this is useful already since you can shove the value into a formatted string formatter.format(Locale.ENGLISH, "%s", asString()); } }
@Override public JsonPrimitiveAssert isEqualTo(Object o) { isNotNull(); JsonElement fromObject = fromObject(o); if(!fromObject.isPrimitive()) { failWithMessage("<%s> is not a json primitive", o.toString()); } if(!actual.equals(fromObject)) { failWithMessage("expected <%s> to be equal to <%s>", o.toString(), actual); } return this; }
public double[] asDoubleArray() { double[] result = new double[size()]; int i=0; for(JsonElement e: this) { result[i++] = e.asPrimitive().asDouble(); } return result; }
public int[] asIntArray() { int[] result = new int[size()]; int i=0; for(JsonElement e: this) { result[i++] = e.asPrimitive().asInt(); } return result; }
private Object translate(JsonElement e) { if(e.isObject()) { JsonObject o = e.asObject(); Map<String,Object> map = new LinkedHashMap<>(); o.forEach((key,value) -> { map.put(key,translate(value)); }); return map; } else if(e.isArray()) { JsonArray a = e.asArray(); List<Object> list = new ArrayList<>(); a.forEach(element -> { list.add(translate(element)); }); return list; } else { return e.asPrimitive().value(); } }
assertTrue(object.get("a","b","c").asPrimitive().asBoolean(), "extract stuff from a nested object"); assertTrue(object.getBoolean("a","b","c"),
@Test public void shouldGetNumber() { JsonPrimitive primitive = primitive(42); assertThat(primitive.asNumber().intValue(), is(42)); }
@Override public boolean isEmpty() { if(value == null) { return true; } else { return StringUtils.isEmpty(asString()); } }
@Override public JsonPrimitiveAssert isNotEqualTo(Object o) { isNotNull(); JsonElement fromObject = fromObject(o); if(!fromObject.isPrimitive()) { failWithMessage("<%s> is not a json primitive", o.toString()); } if(actual.equals(fromObject)) { failWithMessage("expected <%s> to be not equal to <%s>", o.toString(), actual); } return this; } }
/** * Convert json array of arrays of double primitives to a double[][]. * @param lineString 2d JsonArray of doubles * @return a double[][] 2d array of doubles */ public static @Nonnull double[][] fromJsonJLineString(@Nonnull JsonArray lineString) { double[][] result = new double[lineString.size()][lineString.get(0).asArray().size()]; for(int i =0; i<lineString.size();i++) { JsonArray subArray = lineString.get(i).asArray(); for(int j =0; j<subArray.size();j++) { result[i][j] = subArray.get(j).asPrimitive().asDouble(); } } return result; }
public static NSObject toNsObject(JsonPrimitive p) { NSObject o; if(p.isBoolean()) { o = new NSNumber(p.asBoolean()); } else if(p.isNumber()) { if(p.asString().contains(".")) { o = new NSNumber(p.asDouble()); } else { o = new NSNumber(p.asLong()); } } else if(p.isString()) { o = new NSString(p.asString()); } else { o = null; } return o; } }
@Override public long asLong() { if(type == JsonType.number) { return ((Number)value).longValue(); } else { try { return Long.valueOf(asString()); } catch (NumberFormatException e) { throw new JsonTypeMismatchException("not a long '"+value+"'"); } } }