public Object unwrap(Object o) { if (o == null) { return null; } if (!(o instanceof JsonNode)) { return o; } JsonNode e = (JsonNode) o; if (e.isValueNode()) { if (e.isTextual()) { return e.asText(); } else if (e.isBoolean()) { return e.asBoolean(); } else if (e.isInt()) { return e.asInt(); } else if (e.isLong()) { return e.asLong(); } else if (e.isBigDecimal()) { return e.decimalValue(); } else if (e.isDouble()) { return e.doubleValue(); } else if (e.isFloat()) { return e.floatValue(); } else if (e.isBigDecimal()) { return e.decimalValue(); } else if (e.isNull()) { return null; } } return o; }
@Override public AckArgs deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { List<Object> args = new ArrayList<Object>(); AckArgs result = new AckArgs(args); ObjectMapper mapper = (ObjectMapper) jp.getCodec(); JsonNode root = mapper.readTree(jp); AckCallback<?> callback = currentAckClass.get(); Iterator<JsonNode> iter = root.iterator(); int i = 0; while (iter.hasNext()) { Object val; Class<?> clazz = callback.getResultClass(); if (callback instanceof MultiTypeAckCallback) { MultiTypeAckCallback multiTypeAckCallback = (MultiTypeAckCallback) callback; clazz = multiTypeAckCallback.getResultClasses()[i]; } JsonNode arg = iter.next(); if (arg.isTextual() || arg.isBoolean()) { clazz = Object.class; } val = mapper.treeToValue(arg, clazz); args.add(val); i++; } return result; }
@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final ArrayNode simpleTypes = FACTORY.arrayNode(); ret.put(keyword, simpleTypes); final ArrayNode schemas = FACTORY.arrayNode(); ret.put("schemas", schemas); final JsonNode node = schema.get(keyword); final EnumSet<NodeType> set = EnumSet.noneOf(NodeType.class); if (node.isTextual()) // Single type putType(set, node.textValue()); else { // More than one type, and possibly schemas final int size = node.size(); JsonNode element; for (int index = 0; index < size; index++) { element = node.get(index); if (element.isTextual()) putType(set, element.textValue()); else schemas.add(index); } } /* * If all types are there, no need to collect schemas */ if (EnumSet.complementOf(set).isEmpty()) schemas.removeAll(); /* * Note that as this is an enumset, order is guaranteed */ for (final NodeType type : set) simpleTypes.add(type.toString()); return ret; }
@Override public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException { Preconditions.checkInputType("endswith", in, JsonNodeType.STRING); final String text = in.asText(); final List<JsonNode> out = new ArrayList<>(); for (final JsonNode suffix : args.get(0).apply(scope, in)) { if (!suffix.isTextual()) throw new JsonQueryException("1st argument of endswith() must evaluate to string"); out.add(BooleanNode.valueOf(text.endsWith(suffix.asText()))); } return out; }
private static void testJson() { String json1 = "[\"hello\"]"; JsonNode jsonNode = Json.parse(json1); Json.stringify(jsonNode); System.out.println(jsonNode.toString()); System.out.println(Json.stringify(jsonNode)); ObjectNode jsonObject = Json.newObject(); jsonObject.put("hello", jsonNode); jsonObject.put("hello", 56); System.out.println(jsonObject.toString()); List<Object> strings = new ArrayList<Object>(); strings.add("hello"); strings.add("world"); jsonObject.put("test", Json.toJson(strings)); System.out.println(jsonObject.toString()); jsonObject.put("test2", Json.toJson("string")); jsonObject.put("test2", Json.toJson("sshshhs")); List<String> list = new ArrayList<String>(); list.add("hello me"); // ObjectNode objectNode = Json.newObject(); jsonObject.put("me", Json.toJson(list)); System.out.println(jsonObject.toString()); System.out.println(jsonObject.size()); System.out.println(jsonObject.get("test").isArray()); JsonNode jsonNode1 = jsonObject.get("test"); Iterator iterator = jsonNode1.iterator(); while ((iterator.hasNext())) { System.out.println(iterator.next()); } Iterator<JsonNode> iterator1 = jsonObject.iterator(); while (iterator1.hasNext()) { System.out.println("----------------------"); JsonNode jsonNode2 = iterator1.next(); if (jsonNode2.isArray()) { Iterator iterator2 = jsonNode2.iterator(); while ((iterator2.hasNext())) { System.out.println(iterator2.next()); } } else { if (jsonNode2.isTextual()) { System.out.println("String:" + jsonNode2); } } } }
/** * Parses an ID. * * @param node * @return */ protected Object parseId(JsonNode node) { if (node == null || node.isNull()) { return null; } else if (node.isDouble()) { return node.asDouble(); } else if (node.isFloatingPointNumber()) { return node.asDouble(); } else if (node.isInt()) { return node.asInt(); } else if (node.isIntegralNumber()) { return node.asInt(); } else if (node.isLong()) { return node.asLong(); } else if (node.isTextual()) { return node.asText(); } throw new IllegalArgumentException("Unknown id type"); }
/** * Determines whether or not the given {@link JsonNode} matches the given type. This method is * limitted to a few java types only and shouldn't be used to determine with great accuracy * whether or not the types match. * * @param node the {@link JsonNode} * @param type the {@link Class} * @return true if the types match, false otherwise */ protected boolean isMatchingType(JsonNode node, Class<?> type) { if (node.isNull()) { return true; } else if (node.isTextual()) { return String.class.isAssignableFrom(type); } else if (node.isNumber()) { return Number.class.isAssignableFrom(type) || short.class.isAssignableFrom(type) || int.class.isAssignableFrom(type) || long.class.isAssignableFrom(type) || float.class.isAssignableFrom(type) || double.class.isAssignableFrom(type); } else if (node.isArray() && type.isArray()) { return (node.size() > 0) ? isMatchingType(node.get(0), type.getComponentType()) : false; } else if (node.isArray()) { return type.isArray() || Collection.class.isAssignableFrom(type); } else if (node.isBinary()) { return byte[].class.isAssignableFrom(type) || Byte[].class.isAssignableFrom(type) || char[].class.isAssignableFrom(type) || Character[].class.isAssignableFrom(type); } else if (node.isBoolean()) { return boolean.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type); } else if (node.isObject() || node.isPojo()) { return !type.isPrimitive() && !String.class.isAssignableFrom(type) && !Number.class.isAssignableFrom(type) && !Boolean.class.isAssignableFrom(type); } // not sure if it's a matching type return false; }
@SuppressWarnings({"rawtypes", "unchecked"}) private final void parseDefaults( final JsonNode defaults, final VisualStyle style, final VisualLexicon lexicon) { for (final JsonNode vpNode : defaults) { String vpName = vpNode.get(MAPPING_VP).textValue(); final VisualProperty vp = getVisualProperty(vpName, lexicon); final JsonNode value = vpNode.get("value"); if (vp == null || value == null) { continue; } Object parsedValue = null; if (value.isTextual()) { parsedValue = vp.parseSerializableString(value.asText()); } else { parsedValue = vp.parseSerializableString(value.toString()); } style.setDefaultValue(vp, parsedValue); } }
/** * Directly update view object. * * @param view * @param rootNode * @param lexicon */ public Response updateView( final View<? extends CyIdentifiable> view, final JsonNode rootNode, final VisualLexicon lexicon) { for (final JsonNode vpNode : rootNode) { String vpName = vpNode.get(MAPPING_VP).textValue(); final VisualProperty<?> vp = getVisualProperty(vpName, lexicon); final JsonNode value = vpNode.get(MAPPING_DISCRETE_VALUE); if (vp == null || value == null) { continue; } Object parsedValue = null; if (value.isTextual()) { parsedValue = vp.parseSerializableString(value.asText()); } else { parsedValue = vp.parseSerializableString(value.toString()); } view.setVisualProperty(vp, parsedValue); } return Response.ok().build(); }
@Override public KeyValuedColumnType fromJsonNode(JsonNode json) { if (json.isValueNode() || !json.has("value")) { return null; } BaseType keyType = BaseType.fromJson(json, "key"); BaseType valueType = BaseType.fromJson(json, "value"); KeyValuedColumnType keyValueColumnType = new KeyValuedColumnType(keyType, valueType); JsonNode node = null; if ((node = json.get("min")) != null) { keyValueColumnType.setMin(node.asLong()); } if ((node = json.get("max")) != null) { if (node.isLong()) { keyValueColumnType.setMax(node.asLong()); } else if (node.isTextual() && "unlimited".equals(node.asText())) { keyValueColumnType.setMax(Long.MAX_VALUE); } } return keyValueColumnType; }
@Override JsonNode computeNode(JsonPathContext context, JsonNode[] childValues) { JsonNode o = childValues[0]; JsonNode i = index.eval(context).asNode(); if (i.isTextual()) { if (!o.isObject()) { throw new JsonPathRuntimeException( "field selector must apply on an object, not a " + o.getNodeType().toString().toLowerCase(), position); } return o.path(asString(i, "index of selector")); } if (i.isNumber()) { if (!o.isArray()) { throw new JsonPathRuntimeException( "field selector must apply on an array, not a " + o.getNodeType().toString().toLowerCase(), position); } int n = i.asInt(); if (n >= o.size()) { throw new JsonPathRuntimeException( "index out of bound " + n + " > " + (o.size() - 1), position); } if (n < -o.size()) { throw new JsonPathRuntimeException( "index out of bound " + n + " < " + (-o.size()), position); } if (n < 0) { return o.get(o.size() + n); } return o.get(n); } throw new TypeMismatchException(position, JsonNodeType.NUMBER, o, "the index of the array"); }
private Schema asConnectSchema(JsonNode jsonSchema) { if (jsonSchema.isNull()) return null; Schema cached = toConnectSchemaCache.get(jsonSchema); if (cached != null) return cached; JsonNode schemaTypeNode = jsonSchema.get(JsonSchema.SCHEMA_TYPE_FIELD_NAME); if (schemaTypeNode == null || !schemaTypeNode.isTextual()) throw new DataException("Schema must contain 'type' field"); final SchemaBuilder builder; switch (schemaTypeNode.textValue()) { case JsonSchema.BOOLEAN_TYPE_NAME: builder = SchemaBuilder.bool(); break; case JsonSchema.INT8_TYPE_NAME: builder = SchemaBuilder.int8(); break; case JsonSchema.INT16_TYPE_NAME: builder = SchemaBuilder.int16(); break; case JsonSchema.INT32_TYPE_NAME: builder = SchemaBuilder.int32(); break; case JsonSchema.INT64_TYPE_NAME: builder = SchemaBuilder.int64(); break; case JsonSchema.FLOAT_TYPE_NAME: builder = SchemaBuilder.float32(); break; case JsonSchema.DOUBLE_TYPE_NAME: builder = SchemaBuilder.float64(); break; case JsonSchema.BYTES_TYPE_NAME: builder = SchemaBuilder.bytes(); break; case JsonSchema.STRING_TYPE_NAME: builder = SchemaBuilder.string(); break; case JsonSchema.ARRAY_TYPE_NAME: JsonNode elemSchema = jsonSchema.get(JsonSchema.ARRAY_ITEMS_FIELD_NAME); if (elemSchema == null) throw new DataException("Array schema did not specify the element type"); builder = SchemaBuilder.array(asConnectSchema(elemSchema)); break; case JsonSchema.MAP_TYPE_NAME: JsonNode keySchema = jsonSchema.get(JsonSchema.MAP_KEY_FIELD_NAME); if (keySchema == null) throw new DataException("Map schema did not specify the key type"); JsonNode valueSchema = jsonSchema.get(JsonSchema.MAP_VALUE_FIELD_NAME); if (valueSchema == null) throw new DataException("Map schema did not specify the value type"); builder = SchemaBuilder.map(asConnectSchema(keySchema), asConnectSchema(valueSchema)); break; case JsonSchema.STRUCT_TYPE_NAME: builder = SchemaBuilder.struct(); JsonNode fields = jsonSchema.get(JsonSchema.STRUCT_FIELDS_FIELD_NAME); if (fields == null || !fields.isArray()) throw new DataException("Struct schema's \"fields\" argument is not an array."); for (JsonNode field : fields) { JsonNode jsonFieldName = field.get(JsonSchema.STRUCT_FIELD_NAME_FIELD_NAME); if (jsonFieldName == null || !jsonFieldName.isTextual()) throw new DataException("Struct schema's field name not specified properly"); builder.field(jsonFieldName.asText(), asConnectSchema(field)); } break; default: throw new DataException("Unknown schema type: " + schemaTypeNode.textValue()); } JsonNode schemaOptionalNode = jsonSchema.get(JsonSchema.SCHEMA_OPTIONAL_FIELD_NAME); if (schemaOptionalNode != null && schemaOptionalNode.isBoolean() && schemaOptionalNode.booleanValue()) builder.optional(); else builder.required(); JsonNode schemaNameNode = jsonSchema.get(JsonSchema.SCHEMA_NAME_FIELD_NAME); if (schemaNameNode != null && schemaNameNode.isTextual()) builder.name(schemaNameNode.textValue()); JsonNode schemaVersionNode = jsonSchema.get(JsonSchema.SCHEMA_VERSION_FIELD_NAME); if (schemaVersionNode != null && schemaVersionNode.isIntegralNumber()) { builder.version(schemaVersionNode.intValue()); } JsonNode schemaDocNode = jsonSchema.get(JsonSchema.SCHEMA_DOC_FIELD_NAME); if (schemaDocNode != null && schemaDocNode.isTextual()) builder.doc(schemaDocNode.textValue()); JsonNode schemaParamsNode = jsonSchema.get(JsonSchema.SCHEMA_PARAMETERS_FIELD_NAME); if (schemaParamsNode != null && schemaParamsNode.isObject()) { Iterator<Map.Entry<String, JsonNode>> paramsIt = schemaParamsNode.fields(); while (paramsIt.hasNext()) { Map.Entry<String, JsonNode> entry = paramsIt.next(); JsonNode paramValue = entry.getValue(); if (!paramValue.isTextual()) throw new DataException("Schema parameters must have string values."); builder.parameter(entry.getKey(), paramValue.textValue()); } } JsonNode schemaDefaultNode = jsonSchema.get(JsonSchema.SCHEMA_DEFAULT_FIELD_NAME); if (schemaDefaultNode != null) builder.defaultValue(convertToConnect(builder, schemaDefaultNode)); Schema result = builder.build(); toConnectSchemaCache.put(jsonSchema, result); return result; }