@Override public RoaringBitmap deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException { final TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser); final TreeNode bitmapObject = treeNode.get(RoaringBitmapConstants.BITMAP_WRAPPER_OBJECT_NAME); if (null != bitmapObject) { final TextNode jsonNodes = (TextNode) bitmapObject.get(RoaringBitmapConstants.BITMAP_VALUE_FIELD_NAME); return (RoaringBitmap) bitmapSerialiser.deserialise(jsonNodes.binaryValue()); } else { throw new IllegalArgumentException("Received null bitmap treenode"); } }
@Override public CoinbaseProCandle deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); TreeNode jo = mapper.readTree(jp); if (!jo.isArray() || jo.size() != 6) { throw new JsonMappingException(jp, "An array of length 6 expected: " + jo.toString()); } for (int i = 0; i < 6; i++) { if (!(jo.get(i) instanceof NumericNode)) { throw new JsonMappingException(jp, "Numeric values expected: " + jo.toString()); } } return new CoinbaseProCandle( new Date(((NumericNode) jo.get(0)).asLong() * 1000), new BigDecimal(((NumericNode) jo.get(3)).asText()), new BigDecimal(((NumericNode) jo.get(2)).asText()), new BigDecimal(((NumericNode) jo.get(1)).asText()), new BigDecimal(((NumericNode) jo.get(4)).asText()), new BigDecimal(((NumericNode) jo.get(5)).asText())); } }
protected Object _deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { final TreeNode treeNode = p.readValueAsTree(); final TreeNode typeNode = treeNode.path("type"); if (!typeNode.isObject()) { ctxt.reportWrongTokenException(typeNode.traverse(), JsonToken.START_OBJECT, "expected START_OBJECT before the type information and deserialized value"); } final TreeNode valueNode = typeNode.path("@value"); if (!valueNode.isValueNode()) { ctxt.reportWrongTokenException(typeNode.traverse(), JsonToken.VALUE_STRING, "expected VALUE_STRING as type information and deserialized value"); } final JsonParser jsonParser = valueNode.traverse(); final String typeId = jsonParser.nextTextValue(); final JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId); final JsonParser newParser = treeNode.traverse(); if (newParser.nextToken() != JsonToken.START_OBJECT) { ctxt.reportWrongTokenException(newParser, JsonToken.START_OBJECT, "expected START_OBJECT"); } return deser.deserialize(newParser, ctxt); } }
private void hash(TreeNode node) { if (node.isArray()) { md.update((byte)'l'); for (int i = 0; i < node.size(); i++) { hash(node.get(i)); } md.update((byte)'e'); } else if (node.isObject()) { String[] fieldNames = new String[node.size()]; Iterator<String> it = node.fieldNames(); for (int i=0; i< fieldNames.length; i++) { fieldNames[i] = it.next(); } Arrays.sort(fieldNames); md.update((byte)'d'); for (String fName : fieldNames) { hash(fName); hash(node.get(fName)); } md.update((byte)'e'); } else if (node.isValueNode()) { String val = ((JsonNode)node).textValue(); if (val != null) { hash(val); } } else { throw new IllegalArgumentException(node.toString()); } }
@Override public ContentAndMetadata deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser); Preconditions.checkState(treeNode.isArray() && treeNode.size() >= 1, "The response should be an array with at least one element"); Preconditions.checkState(treeNode.get(0).isObject(), "The element should be a Json object"); ObjectNode node = (ObjectNode) treeNode.get(0); return ContentAndMetadata.from(ImmutableList.copyOf(node.fields()) .stream() .collect(Guavate.toImmutableMap(Entry::getKey, entry -> asListOfString(entry.getValue())))); }
final TreeNode jsonRootNode = objectMapper.readTree(resourceSpecificationString); for (int i = 0; i < jsonRootNode.size(); i++) { final TreeNode resourceNode = jsonRootNode.get(i); final String type = resourceNode.get("type").traverse().nextTextValue(); final int memory = resourceNode.get("memory_mb").traverse().getIntValue(); final int capacity = resourceNode.get("capacity").traverse().getIntValue(); final int executorNum = resourceNode.path("num").traverse().nextIntValue(1); final int poisonSec = resourceNode.path("poison_sec").traverse().nextIntValue(-1); resourceRequestCount.getAndAdd(executorNum); containerManager.requestContainer(executorNum, new ResourceSpecification(type, capacity, memory, poisonSec));
/** * @param jsonString serialized bandwidth specification. * @return the bandwidth specification. */ static BandwidthSpecification fromJsonString(final String jsonString) { final BandwidthSpecification specification = new BandwidthSpecification(); try { final ObjectMapper objectMapper = new ObjectMapper(); final TreeNode jsonRootNode = objectMapper.readTree(jsonString); for (int i = 0; i < jsonRootNode.size(); i++) { final TreeNode locationNode = jsonRootNode.get(i); final String name = locationNode.get("name").traverse().nextTextValue(); final int up = locationNode.get("up").traverse().getIntValue(); final int down = locationNode.get("down").traverse().getIntValue(); specification.nodeNames.add(name); specification.uplinkBandwidth.put(name, up); specification.downlinkBandwidth.put(name, down); } } catch (final IOException e) { throw new RuntimeException(e); } return specification; }
/** Converts the provided {@link Struct} into {@link PipelineOptions}. */ public static PipelineOptions fromProto(Struct protoOptions) { try { Map<String, TreeNode> mapWithoutUrns = new HashMap<>(); TreeNode rootOptions = MAPPER.readTree(JsonFormat.printer().print(protoOptions)); Iterator<String> optionsKeys = rootOptions.fieldNames(); while (optionsKeys.hasNext()) { String optionKey = optionsKeys.next(); TreeNode optionValue = rootOptions.get(optionKey); mapWithoutUrns.put( CaseFormat.LOWER_UNDERSCORE.to( CaseFormat.LOWER_CAMEL, optionKey.substring("beam:option:".length(), optionKey.length() - ":v1".length())), optionValue); } return MAPPER.readValue( MAPPER.writeValueAsString(ImmutableMap.of("options", mapWithoutUrns)), PipelineOptions.class); } catch (IOException e) { throw new RuntimeException("Failed to read PipelineOptions from Protocol", e); } }
if (referenceNode != null && referenceNode.isObject()) { TreeNode serviceIdNode = containerNode.get("serviceId"); if (serviceIdNode == null) { TreeNode referenceServiceIdNode = referenceNode.get("serviceId"); if (referenceServiceIdNode != null && referenceServiceIdNode.isValueNode()) { containerNode.set("serviceId", (ValueNode) referenceServiceIdNode);
final TreeNode readTree = jp.getCodec().readTree(jp); final String typesNodeName = "@types"; final TreeNode typesNodes = readTree.get(typesNodeName); if (typesNodes != null) { final Iterator<String> typeFields = typesNodes.fieldNames(); while (typeFields.hasNext()) { final String key = typeFields.next(); final TreeNode typeNode = typesNodes.get(key); if (typeNode instanceof TextNode) { final String type = ((TextNode) typeNode).textValue(); final Iterator<String> fieldNames = readTree.fieldNames(); while (fieldNames.hasNext()) { final String key = fieldNames.next(); if (!key.equals(typesNodeName)) { final TreeNode fieldNode = readTree.get(key); final JsonParser fieldValueParser = fieldNode.traverse(); fieldValueParser.setCodec(jp.getCodec());
private Optional<Domain> domain(TreeNode node) throws IOException, JsonParseException { TreeNode value = node.get(QUOTA_ROOT_DOMAIN_FIELD); if (value == null || value.isMissingNode()) { return Optional.empty(); } return Optional.ofNullable(node.asToken().asString()).map(Domain::of); }
protected Class<?> determineConcreteType(TreeNode node) { TreeNode entityNode = node.get(ConstantsV1.ENTITY_TYPE); if (entityNode != null && entityNode.isValueNode()) { String entityType = ((ValueNode) entityNode).asText(); Class<?> klazz = NuxeoConverterFactory.entityTypeToClass.get(entityType); if (klazz != null) { return klazz; } } return Object.class; }
@Override public List<Pair<AccountName, Long>> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { List<Pair<AccountName, Long>> result = new ArrayList<>(); ObjectCodec codec = jsonParser.getCodec(); TreeNode rootNode = codec.readTree(jsonParser); if (rootNode.isArray()) { for (JsonNode node : (ArrayNode) rootNode) { // result.put((node.get(0)).asText(), (node.get(0)).asInt()); } return result; } throw new IllegalArgumentException("JSON Node is not an array."); } }
@SuppressWarnings("unchecked") private static int nodeSize(Object node) { return ((TreeNode) node).size(); }
public class StringListDeserializer extends JsonDeserializer<List<String>>{ @Override public List<String> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { List<String> ret = new ArrayList<>(); ObjectCodec codec = parser.getCodec(); TreeNode node = codec.readTree(parser); if (node.isArray()){ for (JsonNode n : (ArrayNode)node){ ret.add(n.asText()); } } else if (node.isValueNode()){ ret.add( ((JsonNode)node).asText() ); } return ret; } }
public boolean parse() throws IOException, JsonProcessingException { JsonParser parser=factory.createParser(input); boolean done=false; do { TreeNode t=parser.readValueAsTree(); if(t==null) { done=true; } else { if(t instanceof ObjectNode) { if(!documentCompleted((ObjectNode)t)) return false; } else if(t instanceof ArrayNode) { int n=t.size(); for(int i=0;i<n;i++) { TreeNode elem=t.path(i); if(elem!=null) { if(!documentCompleted((ObjectNode)elem)) return false; } } } else { throw new RuntimeException("Ill formed stream"); } } } while(!done); return true; }
/** * Convenience method for traversing over given {@link TreeNode} by exposing * contents as a {@link JsonParser}. *<p> * NOTE! Returned parser has not been advanced to the first token; caller has to * do this. */ default JsonParser treeAsTokens(TreeNode n) { return n.traverse(this); }
/** * Set the function description of a transformation. Requires an object behind function_description. * * @param jp the current json parser * @param transformation the target {@code Transformation} * @throws IOException */ private static void setFunctionDescription(final JsonParser jp, final Transformation transformation) throws IOException { final TreeNode treeNode = jp.readValueAsTree(); if (treeNode.isObject()) { transformation.setFunctionDescription((com.fasterxml.jackson.databind.node.ObjectNode) treeNode); } else { throw JsonMappingException.from(jp, String.format( "Cannot parse the function description for the Transformation [%s], because it is not a Json Object", transformation)); } }