/** * Use {@link #isString()} to check if string (it is not possible to check if a character, however). */ public Character asChar() { return getChar(null, asJsonNode()); }
/** * Use {@link #isInt()} to check first, if required. */ public Integer asInt() { return getInt(null, asJsonNode()); }
/** * Use {@link #isString()} to check first, if required. */ public String asString() { return getString(null, asJsonNode()); }
/** * Either returns a {@link JsonRepresentation} that indicates that the * wrapped node has <tt>null</tt> value (ie * {@link JsonRepresentation#isNull()}), or returns <tt>null</tt> if there * was no node with the provided path. * * <p> * Use {@link #isNull()} to check first, if required. */ public JsonRepresentation asNull() { return getNull(null, asJsonNode()); }
@Override public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { final JsonRepresentation jsonRepresentation = (JsonRepresentation) value; final JsonNode jsonNode = jsonRepresentation.asJsonNode(); jgen.writeTree(jsonNode); } }
/** * Use {@link #isBigDecimal()} to check first, if required. */ public BigDecimal asBigDecimal(String format) { return getBigDecimal(null, format, asJsonNode()); }
/** * Convert underlying representation into an array. */ protected ArrayNode asArrayNode() { if (!isArray()) { throw new IllegalStateException("does not represent array"); } return (ArrayNode) asJsonNode(); }
/** * Use {@link #isBoolean()} to check first, if required. */ public Boolean asBoolean() { return getBoolean(null, asJsonNode()); }
/** * Use {@link #isLong()} to check first, if required. */ public Long asLong() { return getLong(null, asJsonNode()); }
/** * Use {@link #isNumber()} to test if number (it is not possible to check if a float, however). */ public Float asFloat() { return getFloat(null, asJsonNode()); }
/** * Use {@link #isLink()} to check first, if required. */ public LinkRepresentation asLink() { return getLink(null, asJsonNode()); }
/** * Use {@link #isIntegralNumber()} to test if number (it is not possible to check if a byte, however). */ public Byte asByte() { return getByte(null, asJsonNode()); }
/** * Use {@link #isDouble()} to check first, if required. */ public Double asDouble() { return getDouble(null, asJsonNode()); }
/** * Convert underlying representation into an object (map). */ protected ObjectNode asObjectNode() { if (!isMap()) { throw new IllegalStateException("does not represent map"); } return (ObjectNode) asJsonNode(); }
/** * Convert a representation that contains a single node representing a link * into a {@link LinkRepresentation}. */ public LinkRepresentation mapValueAsLink() { if (asJsonNode().size() != 1) { throw new IllegalStateException("does not represent link"); } final String linkPropertyName = asJsonNode().getFieldNames().next(); return getLink(linkPropertyName); }
public String asArg() { if (isValue()) { return asJsonNode().getValueAsText(); } else { return asJsonNode().toString(); } }
public JsonRepresentation arraySetElementAt(final int i, final JsonRepresentation objectRepr) { ensureIsAnArrayAtLeastAsLargeAs(i+1); if (objectRepr.isArray()) { throw new IllegalArgumentException("Representation being set cannot be an array"); } // can safely downcast because *this* representation is an array final ArrayNode arrayNode = (ArrayNode) jsonNode; arrayNode.set(i, objectRepr.asJsonNode()); return this; }
@Test public void forJsonRepresentation() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final Iterable<Entry<String, JsonRepresentation>> mapIterable = jsonRepresentation.mapIterable(); final Iterator<Entry<String, JsonRepresentation>> mapIterator = mapIterable.iterator(); for (int i = 0; i < jsonRepresentation.asJsonNode().size(); i++) { assertThat(mapIterator.hasNext(), is(true)); assertThat(mapIterator.next().getKey(), is(not(nullValue()))); } assertThat(mapIterator.hasNext(), is(false)); }
public JsonRepresentation mapPut(final String key, final JsonRepresentation value) { if (!isMap()) { throw new IllegalStateException("does not represent map"); } if (value == null) { return this; } final Path path = Path.parse(key); final ObjectNode node = JsonNodeUtils.walkNodeUpTo(asObjectNode(), path.getHead()); node.put(path.getTail(), value.asJsonNode()); return this; }
@Test public void forJsonRepresentation() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final Iterable<Entry<String, JsonRepresentation>> mapIterable = jsonRepresentation.mapIterable(); final Iterator<Entry<String, JsonRepresentation>> mapIterator = mapIterable.iterator(); for (int i = 0; i < jsonRepresentation.asJsonNode().size(); i++) { assertThat(mapIterator.hasNext(), is(true)); assertThat(mapIterator.next().getKey(), is(not(nullValue()))); } assertThat(mapIterator.hasNext(), is(false)); }