@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 JsonElementAssert isPrimitive() { isNotNull(); if(!actual.isPrimitive()) { failWithMessage("should be primitive but was <%s>", actual.type()); } return this; }
@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; } }
/** * Convenient method providing a few alternate ways of extracting elements * from a JsonArray. * * @param label label * @return the first element in the array matching the label or the n-th * element if the label is an integer and the element an object or * an array. */ public JsonElement get(String label) { int i = 0; try{ for (JsonElement e : this) { if(e.isPrimitive() && e.asPrimitive().asString().equals(label)) { return e; } else if((e.isObject() || e.isArray()) && Integer.valueOf(label).equals(i)) { return e; } i++; } } catch(NumberFormatException e) { // fail gracefully return null; } // the element was not found return null; }
/** * @param field * name of the field * @param defaultValue * default value that is returned if the field has no value * @return value of the field as a boolean */ default boolean get(@Nonnull String field, boolean defaultValue) { JsonElement e = get(field); if (e == null) { return defaultValue; } else { if (e.isBoolean()) { return e.asBoolean(); } else if (e.isNumber()) { return e.asInt() > 0; } else if (e.isPrimitive()) { return Boolean.valueOf(e.asString()); } else { throw new JsonTypeMismatchException("expected primitive value but was " + e.type()); } } }
public boolean endObjectEntry() { JsonElement value = stack.pollLast(); JsonElement e = stack.peekLast(); if (e.isPrimitive()) { // field name e = stack.pollLast(); JsonObject container = stack.peekLast().asObject(); String key = e.asPrimitive().asString(); container.put(key, value); if(container.size()>settings.upgradeThresholdToMapBasedJsonObject() && !MapBasedJsonObject.class.equals(container.getClass())) { JsonElement removed = stack.pollLast(); MapBasedJsonObject newContainer = new MapBasedJsonObject(removed.asObject()); stack.add(newContainer); } } return true; }
public void shouldAddJsonElements() { JsonArray arr = new JsonArray(); arr.add(primitive(1),object(field("1", "2")),array(42)); assertThat(arr.size(), is(3)); assertThat(arr.get(0).isPrimitive(), is(true)); assertThat(arr.get(1).isObject(), is(true)); assertThat(arr.get(2).isArray(), is(true)); }
/** * Get a value at a particular path in an object structure. * * @param labels * one or more text labels * @return value or null if it doesn't exist at the specified path */ default Boolean getBoolean(final String... labels) { JsonElement jsonElement = get(labels); if (jsonElement == null || jsonElement.isNull()) { return null; } else { if (jsonElement.isBoolean()) { return jsonElement.asBoolean(); } else if (jsonElement.isNumber()) { return jsonElement.asInt() > 0; } else if (jsonElement.isPrimitive()) { return Boolean.valueOf(jsonElement.asString()); } else { throw new JsonTypeMismatchException("expected primitive value but was " + jsonElement.type()); } } }