public JsonElementAssert isPrimitive() { isNotNull(); if(!actual.isPrimitive()) { failWithMessage("should be primitive but was <%s>", actual.type()); } return this; }
public JsonElementAssert isObject() { isNotNull(); if(!actual.isObject()) { failWithMessage("should be object but was <%s>", actual.type()); } return this; }
public JsonElementAssert isArray() { isNotNull(); if(!actual.isArray()) { failWithMessage("should be array but was <%s>", actual.type()); } return this; }
return; JsonType type = json.type(); switch (type) { case object:
/** * @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()); } } }
protected void flatten(@Nonnull JsonObject root, @Nonnull String path, @Nonnull String separator, JsonElement element) { JsonType type = element.type(); switch (type) { case array: JsonArray arr = element.asArray(); for(int i=0; i<arr.size();i++) { if(path.length()>0) { flatten(root,path+separator+i,separator,arr.get(i)); } else { flatten(root,""+i,separator,arr.get(i)); } } break; case object: if(path.length()>0) { element.asObject().forEach((key, value) -> flatten(root,path+separator+key,separator,value)); } else { element.asObject().forEach((key, value) -> flatten(root,key,separator,value)); } break; default: root.put(path, element); break; } }
/** * 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()); } } }
private void serialize(YAMLGenerator yaml, JsonElement e) throws IOException { switch (e.type()) { case object: yaml.writeStartObject();