List of usage examples for com.fasterxml.jackson.databind.node JsonNodeFactory instance
JsonNodeFactory instance
To view the source code for com.fasterxml.jackson.databind.node JsonNodeFactory instance.
Click Source Link
From source file:org.apache.solr.kelvin.scorer.ScorersTest.java
public void testEventCollector() throws Exception { EventCollectorScorer S = new EventCollectorScorer(); S.configure(JsonNodeFactory.instance.objectNode()); S.update(null, new NullPointerException()); S.update(null, new NullPointerException()); S.update(null, new NullPointerException()); S.update(null, new IOException()); S.update(null, new IOException()); S.update(null, new ExceptionTestEvent(null, null, new IllegalAccessError())); assertEquals(3, S.measureReport().size()); assertEquals(3.0, peek(S, "NullPointerException").getValue()); assertEquals(2.0, peek(S, "IOException").getValue()); assertEquals(1.0, peek(S, "IllegalAccessError").getValue()); }
From source file:com.redhat.lightblue.util.JsonUtils.java
/** * Returns a Json value node for the given value. Dates are converted to strings *//* w ww . java2s . c om*/ public static ValueNode valueToJson(Object value) { if (value == null) { return JsonNodeFactory.instance.nullNode(); } else if (value instanceof String) { return JsonNodeFactory.instance.textNode((String) value); } else if (value instanceof Number) { if (value instanceof BigDecimal) { return JsonNodeFactory.instance.numberNode((BigDecimal) value); } else if (value instanceof BigInteger) { return JsonNodeFactory.instance.numberNode((BigInteger) value); } else if (value instanceof Double) { return JsonNodeFactory.instance.numberNode((Double) value); } else if (value instanceof Float) { return JsonNodeFactory.instance.numberNode((Float) value); } else if (value instanceof Long) { return JsonNodeFactory.instance.numberNode((Long) value); } else { return JsonNodeFactory.instance.numberNode(((Number) value).intValue()); } } else if (value instanceof Boolean) { return JsonNodeFactory.instance.booleanNode(((Boolean) value).booleanValue()); } else if (value instanceof Date) { return JsonNodeFactory.instance.textNode(Constants.getDateFormat().format((Date) value)); } else { return JsonNodeFactory.instance.textNode(value.toString()); } }
From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java
@Test public void mapToJsonStringKeys() { Schema stringIntMap = SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).build(); Map<String, Integer> input = new HashMap<>(); input.put("key1", 12); input.put("key2", 15); JsonNode converted = converter.fromConnectData(stringIntMap, input); assertEquals(JsonNodeFactory.instance.objectNode().put("key1", 12).put("key2", 15), converted); }
From source file:io.gs2.auth.Gs2AuthClient.java
/** * ????<br>/* w w w . ja v a 2s . c om*/ * <br> * * @param request * @return ? */ public LoginResult login(LoginRequest request) { ObjectNode body = JsonNodeFactory.instance.objectNode().put("serviceId", request.getServiceId()) .put("userId", request.getUserId()); HttpPost post = createHttpPost(Gs2Constant.ENDPOINT_HOST + "/login", credential, ENDPOINT, LoginRequest.Constant.MODULE, LoginRequest.Constant.FUNCTION, body.toString()); if (request.getRequestId() != null) { post.setHeader("X-GS2-REQUEST-ID", request.getRequestId()); } return doRequest(post, LoginResult.class); }
From source file:com.turn.shapeshifter.NamedSchemaSerializer.java
/** * Serializes a repeated mapped field./*from w ww .j a v a 2 s.co m*/ * * @param message the message being serialized * @param registry a registry of schemas, for enclosed object types * @param field the descriptor of the repeated field to serialize * @param count the count of repeated items in the field * @return the JSON representation of the serialized mapped field * @throws SerializationException * @see {@link NamedSchema#mapRepeatedField(String, String)} */ private ObjectNode serializeMappedField(Message message, ReadableSchemaRegistry registry, FieldDescriptor field, int count) throws SerializationException { ObjectNode objectNode = new ObjectNode(JsonNodeFactory.instance); for (int i = 0; i < count; i++) { Message value = (Message) message.getRepeatedField(field, i); String key = String.valueOf(value.getField(schema.getMappings().get(field.getName()))); JsonNode valueNode = serializeValue(value, field, registry); if (!valueNode.isNull() && key != null) { objectNode.put(key, valueNode); } } return objectNode; }
From source file:com.msopentech.odatajclient.testservice.utils.Commons.java
public static InputStream getLinksAsJSON(final String entitySetName, final Map.Entry<String, Collection<String>> link) throws IOException { final ObjectNode links = new ObjectNode(JsonNodeFactory.instance); links.put(JSON_ODATAMETADATA_NAME, ODATA_METADATA_PREFIX + entitySetName + "/$links/" + link.getKey()); final ArrayNode uris = new ArrayNode(JsonNodeFactory.instance); for (String uri : link.getValue()) { final String absoluteURI; if (URI.create(uri).isAbsolute()) { absoluteURI = uri;/*from w ww . j a v a2 s .c om*/ } else { absoluteURI = DEFAULT_SERVICE_URL + uri; } uris.add(new ObjectNode(JsonNodeFactory.instance).put("url", absoluteURI)); } if (uris.size() == 1) { links.setAll((ObjectNode) uris.get(0)); } else { links.set("value", uris); } return IOUtils.toInputStream(links.toString()); }
From source file:org.springframework.cloud.aws.messaging.support.converter.NotificationRequestConverterTest.java
@Test public void testNoMessageAvailableSupplied() throws Exception { this.expectedException.expect(MessageConversionException.class); this.expectedException.expectMessage("does not contain a message"); ObjectNode jsonObject = JsonNodeFactory.instance.objectNode(); jsonObject.put("Type", "Notification"); jsonObject.put("Subject", "Hello World!"); String payload = jsonObject.toString(); new NotificationRequestConverter().fromMessage(MessageBuilder.withPayload(payload).build(), null); }
From source file:com.yahoo.elide.graphql.GraphQLEndpoint.java
/** * Create handler.//from www . j a v a 2 s . c o m * * @param securityContext security context * @param graphQLDocument post data as jsonapi document * @return response */ @POST @Consumes(MediaType.APPLICATION_JSON) public Response post(@Context SecurityContext securityContext, String graphQLDocument) { ObjectMapper mapper = elide.getMapper().getObjectMapper(); JsonNode topLevel; try { topLevel = mapper.readTree(graphQLDocument); } catch (IOException e) { log.debug("Invalid json body provided to GraphQL", e); // NOTE: Can't get at isVerbose setting here for hardcoding to false. If necessary, we can refactor // so this can be set appropriately. return buildErrorResponse(new InvalidEntityBodyException(graphQLDocument), false); } Function<JsonNode, Response> executeRequest = (node) -> executeGraphQLRequest(mapper, securityContext, graphQLDocument, node); if (topLevel.isArray()) { Iterator<JsonNode> nodeIterator = topLevel.iterator(); Iterable<JsonNode> nodeIterable = () -> nodeIterator; // NOTE: Create a non-parallel stream // It's unclear whether or not the expectations of the caller would be that requests are intended // to run serially even outside of a single transaction. We should revisit this. Stream<JsonNode> nodeStream = StreamSupport.stream(nodeIterable.spliterator(), false); ArrayNode result = nodeStream.map(executeRequest).map(response -> { try { return mapper.readTree((String) response.getEntity()); } catch (IOException e) { log.debug("Caught an IO exception while trying to read response body"); return JsonNodeFactory.instance.objectNode(); } }).reduce(JsonNodeFactory.instance.arrayNode(), (arrayNode, node) -> arrayNode.add(node), (left, right) -> left.addAll(right)); try { return Response.ok(mapper.writeValueAsString(result)).build(); } catch (IOException e) { log.error("An unexpected error occurred trying to serialize array response.", e); return Response.serverError().build(); } } return executeRequest.apply(topLevel); }
From source file:org.attribyte.api.pubsub.impl.server.APIServlet.java
private void doTopicsGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { int pageSize = ServerUtil.getParameter(request, "pageSize", DEFAULT_PAGE_SIZE); if (pageSize < 1) pageSize = DEFAULT_PAGE_SIZE;//from www . j a va2s . com try { List<Topic> topics = datastore.getTopics(0, pageSize); ObjectNode responseNode = JsonNodeFactory.instance.objectNode(); ArrayNode topicsNode = responseNode.putArray("topics"); for (Topic topic : topics) { ObjectNode topicObj = topicsNode.addObject(); topicObj.put("name", topic.getURL()); topicObj.put("id", topic.getId()); } sendJSONResponse(response, responseNode); } catch (DatastoreException de) { logger.error("Problem selecting topics", de); sendServletResponse(INTERNAL_ERROR_RESPONSE, response); } }
From source file:com.heliosapm.tsdblite.handlers.websock.WebSocketRequest.java
/** * Sends the passed JSON node as the response to this request * The <b><code>rerid</code></b> is added to the response so the caller * can correlate it's request to this response. * @param responseNode The response node. * @return The write future/*w w w . j a v a 2s. com*/ */ public ChannelFuture sendResponse(final ObjectNode responseNode) { if (responseNode == null) throw new IllegalArgumentException("The passed ObjectNode was null"); responseNode.set("rerid", JsonNodeFactory.instance.numberNode(rid)); return ctx.writeAndFlush(new TextWebSocketFrame(JSON.serializeToBuf(responseNode))); }