Java Code Examples for com.fasterxml.jackson.databind.node.NullNode#getInstance()
The following examples show how to use
com.fasterxml.jackson.databind.node.NullNode#getInstance() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectToJsonNode.java From yosegi with Apache License 2.0 | 6 votes |
/** * Judge Java objects and create JsonNode. */ public static JsonNode get( final Object obj ) throws IOException { if ( obj instanceof PrimitiveObject ) { return PrimitiveObjectToJsonNode.get( (PrimitiveObject)obj ); } else if ( obj instanceof String ) { return new TextNode( (String)obj ); } else if ( obj instanceof Boolean ) { return BooleanNode.valueOf( (Boolean)obj ); } else if ( obj instanceof Short ) { return IntNode.valueOf( ( (Short)obj ).intValue() ); } else if ( obj instanceof Integer ) { return IntNode.valueOf( (Integer)obj ); } else if ( obj instanceof Long ) { return new LongNode( (Long)obj ); } else if ( obj instanceof Float ) { return new DoubleNode( ( (Float)obj ).doubleValue() ); } else if ( obj instanceof Double ) { return new DoubleNode( (Double)obj ); } else if ( obj instanceof byte[] ) { return new BinaryNode( (byte[])obj ); } else if ( obj == null ) { return NullNode.getInstance(); } else { return new TextNode( obj.toString() ); } }
Example 2
Source File: IRequestParameter.java From netty-rest with Apache License 2.0 | 6 votes |
public Object extract(ObjectNode node, RakamHttpRequest request) { JsonNode value = node.get(name); Object o; if (value == null) { o = null; } else { try { o = mapper.convertValue(value, type); } catch (IllegalArgumentException e) { throw new HttpRequestException(name + " body parameter cannot be cast to " + type.toString() + ": "+e.getMessage(), BAD_REQUEST); } } if (required && (o == null || o == NullNode.getInstance())) { throw new HttpRequestException(name + " body parameter is required", BAD_REQUEST); } return o; }
Example 3
Source File: Json.java From immutables with Apache License 2.0 | 6 votes |
JsonNode valueOrNull(String name) { Objects.requireNonNull(name, "name"); if (fields != null && fields.has(name)) { JsonNode field = fields.get(name); if (field.isArray()) { // return first element (or null) Iterator<JsonNode> iter = field.elements(); return iter.hasNext() ? iter.next() : null; } return field; } JsonNode found = source().at(name); return found.isMissingNode() ? NullNode.getInstance() : found; }
Example 4
Source File: ElasticsearchClient.java From presto with Apache License 2.0 | 5 votes |
private JsonNode nullSafeNode(JsonNode jsonNode, String name) { if (jsonNode == null || jsonNode.isNull() || jsonNode.get(name) == null) { return NullNode.getInstance(); } return jsonNode.get(name); }
Example 5
Source File: JacksonMessageWriter.java From yosegi with Apache License 2.0 | 5 votes |
@Override public byte[] create( final IParser parser ) throws IOException { JsonNode jsonNode; if ( parser == null ) { jsonNode = NullNode.getInstance(); } else if ( parser.isMap() || parser.isStruct() ) { jsonNode = JacksonParserToJsonObject.getFromObjectParser( parser ); } else if ( parser.isArray() ) { jsonNode = JacksonParserToJsonObject.getFromArrayParser( parser ); } else { jsonNode = NullNode.getInstance(); } return objectMapper.writeValueAsBytes( jsonNode ); }
Example 6
Source File: PrimitiveObjectToJsonNode.java From yosegi with Apache License 2.0 | 5 votes |
/** * Convert PrimitiveObject to JsonNode. */ public static JsonNode get( final PrimitiveObject obj ) throws IOException { if ( obj == null ) { return NullNode.getInstance(); } switch ( obj.getPrimitiveType() ) { case BOOLEAN: return BooleanNode.valueOf( obj.getBoolean() ); case BYTE: return IntNode.valueOf( obj.getInt() ); case SHORT: return IntNode.valueOf( obj.getInt() ); case INTEGER: return IntNode.valueOf( obj.getInt() ); case LONG: return new LongNode( obj.getLong() ); case FLOAT: return new DoubleNode( obj.getDouble() ); case DOUBLE: return new DoubleNode( obj.getDouble() ); case STRING: return new TextNode( obj.getString() ); case BYTES: return new BinaryNode( obj.getBytes() ); default: return NullNode.getInstance(); } }
Example 7
Source File: JsonUtil.java From mirror with Apache License 2.0 | 5 votes |
public static JsonNode toJsonNode(String jsonString) { try { return objectMapper.readTree(jsonString); } catch (IOException e) { Timber.e(e, "Error parsing toJsonNode"); return NullNode.getInstance(); } }
Example 8
Source File: BasicHeaderTest.java From java-jwt with MIT License | 5 votes |
@Test public void shouldHaveTree() throws Exception { HashMap<String, JsonNode> map = new HashMap<>(); JsonNode node = NullNode.getInstance(); map.put("key", node); BasicHeader header = new BasicHeader(null, null, null, null, map, objectReader); assertThat(header.getTree(), is(notNullValue())); assertThat(header.getTree(), is(IsMapContaining.hasEntry("key", node))); }
Example 9
Source File: HeaderDeserializerTest.java From java-jwt with MIT License | 5 votes |
@Test public void shouldGetNullStringWhenParsingNullNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); NullNode node = NullNode.getInstance(); tree.put("key", node); String text = deserializer.getString(tree, "key"); assertThat(text, is(nullValue())); }
Example 10
Source File: JsonNodeClaimTest.java From java-jwt with MIT License | 5 votes |
@Test public void shouldReturnBaseClaimWhenParsingNullNode() throws Exception { JsonNode value = NullNode.getInstance(); Claim claim = claimFromNode(value); assertThat(claim, is(notNullValue())); assertThat(claim, is(instanceOf(NullClaim.class))); assertThat(claim.isNull(), is(true)); }
Example 11
Source File: ToTupleConverter.java From immutables with Apache License 2.0 | 5 votes |
@Override public ProjectedTuple convert(JsonNode node) { List<Object> values = new ArrayList<>(); for (ProjectedField info: fields) { JsonNode value = node.path(info.pathAsString); if (value.isMissingNode()) { value = NullNode.getInstance(); } values.add(info.convert(value)); } return ProjectedTuple.of(query.projections(), values); }
Example 12
Source File: ErrorResponse.java From simple-json-rpc with MIT License | 4 votes |
public ErrorResponse(@NotNull ErrorMessage error) { super(NullNode.getInstance()); this.error = error; }
Example 13
Source File: JsonConverter.java From BIMserver with GNU Affero General Public License v3.0 | 4 votes |
public JsonNode toJson(Object object) throws IOException { if (object instanceof SBase) { SBase base = (SBase) object; ObjectNode jsonObject = OBJECT_MAPPER.createObjectNode(); jsonObject.put("__type", base.getSClass().getSimpleName()); for (SField field : base.getSClass().getAllFields()) { jsonObject.set(field.getName(), toJson(base.sGet(field))); } return jsonObject; } else if (object instanceof Collection) { Collection<?> collection = (Collection<?>) object; ArrayNode jsonArray = OBJECT_MAPPER.createArrayNode(); for (Object value : collection) { jsonArray.add(toJson(value)); } return jsonArray; } else if (object instanceof Date) { return new LongNode(((Date) object).getTime()); } else if (object instanceof DataHandler) { DataHandler dataHandler = (DataHandler) object; InputStream inputStream = dataHandler.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(inputStream, out); return new TextNode(new String(Base64.encodeBase64(out.toByteArray()), Charsets.UTF_8)); } else if (object instanceof Boolean) { return BooleanNode.valueOf((Boolean) object); } else if (object instanceof String) { return new TextNode((String) object); } else if (object instanceof Long) { return new LongNode((Long) object); } else if (object instanceof UUID) { return new TextNode(((UUID) object).toString()); } else if (object instanceof Integer) { return new IntNode((Integer) object); } else if (object instanceof Double) { return new DoubleNode((Double) object); } else if (object instanceof Float) { return new FloatNode((Float) object); } else if (object instanceof Enum) { return new TextNode(object.toString()); } else if (object == null) { return NullNode.getInstance(); } else if (object instanceof byte[]) { byte[] data = (byte[]) object; return new TextNode(new String(Base64.encodeBase64(data), Charsets.UTF_8)); } throw new UnsupportedOperationException(object.getClass().getName()); }
Example 14
Source File: UnionsWithNullTest.java From raml-java-tools with Apache License 2.0 | 3 votes |
@Test public void withJsonNodeNull() throws IOException { JsonNode node = NullNode.getInstance(); ObjectMapper mapper = new ObjectMapper(); NilUnionType b = mapper.treeToValue(node, NilUnionType.class); assertTrue(b.isNil()); }