@Deprecated public static ValueNode toValueNode(Object value) { if (value == null) return NullNode.instance; if (value instanceof ValueNode) return (ValueNode) value; if (value instanceof Boolean) return BooleanNode.valueOf((boolean) value); else if (value instanceof Integer) return IntNode.valueOf((int) value); else if (value instanceof Long) return LongNode.valueOf((long) value); else if (value instanceof Double) return DoubleNode.valueOf((double) value); else if (value instanceof Float) return FloatNode.valueOf((float) value); return TextNode.valueOf(value.toString()); }
public static ValueNode toValueNode(Object value) { if (value == null) return NullNode.instance; if (value instanceof ValueNode) return (ValueNode) value; if (value instanceof Boolean) return BooleanNode.valueOf((boolean) value); else if (value instanceof Integer) return IntNode.valueOf((int) value); else if (value instanceof Long) return LongNode.valueOf((long) value); else if (value instanceof Double) return DoubleNode.valueOf((double) value); else if (value instanceof Float) return FloatNode.valueOf((float) value); return TextNode.valueOf(value.toString()); }
private JsonNode createNode(Object value) { if (value instanceof Float) { return new FloatNode((Float) value); } else if (value instanceof Double) { return new DoubleNode((Double) value); } return null; }
/** * Safely serialize a value. * * @since 1.11.2 * @param encoder The <code>StenoEncoder</code> instance. * @param value The <code>Object</code> instance to safely serialize. */ public static void safeEncodeValue(final StringBuilder encoder, @Nullable final Object value) { if (value == null) { encoder.append("null"); } else if (value instanceof Map) { safeEncodeMap(encoder, (Map<?, ?>) value); } else if (value instanceof List) { safeEncodeList(encoder, (List<?>) value); } else if (value.getClass().isArray()) { safeEncodeArray(encoder, value); } else if (value instanceof LogValueMapFactory.LogValueMap) { safeEncodeLogValueMap(encoder, (LogValueMapFactory.LogValueMap) value); } else if (value instanceof Throwable) { safeEncodeThrowable(encoder, (Throwable) value); } else if (StenoSerializationHelper.isSimpleType(value)) { if (value instanceof Boolean) { encoder.append(BooleanNode.valueOf((Boolean) value).toString()); } else if (value instanceof Double) { encoder.append(DoubleNode.valueOf((Double) value).toString()); } else if (value instanceof Float) { encoder.append(FloatNode.valueOf((Float) value).toString()); } else if (value instanceof Long) { encoder.append(LongNode.valueOf((Long) value).toString()); } else if (value instanceof Integer) { encoder.append(IntNode.valueOf((Integer) value).toString()); } else { encoder.append(new TextNode(value.toString()).toString()); } } else { safeEncodeValue(encoder, LogReferenceOnly.of(value).toLogValue()); } }
public static Object unwrap(Object val) { // Can Jackson do this via // ObjectMapper.treeToValue()? The // spec is unclear Object result = val; ObjectMapper mapper = new ObjectMapper(); if (val instanceof ObjectNode) { result = mapper.convertValue((ObjectNode) val, Map.class); } else if (val instanceof ArrayNode) { result = mapper.convertValue((ObjectNode) val, List.class); } else if (val instanceof NullNode) { result = null; } else if (val instanceof BooleanNode) { result = ((BooleanNode) val).booleanValue(); } else if (val instanceof ShortNode) { result = ((ShortNode) val).shortValue(); } else if (val instanceof IntNode) { result = ((IntNode) val).intValue(); } else if (val instanceof LongNode) { result = ((LongNode) val).longValue(); } else if (val instanceof DoubleNode) { result = ((DoubleNode) val).doubleValue(); } else if (val instanceof FloatNode) { result = ((FloatNode) val).floatValue(); } else if (val instanceof BigIntegerNode) { result = ((BigIntegerNode) val).bigIntegerValue(); } else if (val instanceof DecimalNode) { result = ((DecimalNode) val).decimalValue(); } return result; }
private FloatNode decodeFloat() { return new FloatNode(this.threadBuffer.get().getFloat()); }
private JsonNode generateValue(LinkedList<String> path, ObjectNode schema, int index) { String stringPath = Utils.join(".", path.toArray(new String[path.size()])); List<Object> list = paths.get(stringPath); if (list != null) return Json7.toNode(list.get(random.nextInt(list.size()))); JsonNode values = Json7.get(schema, "_values"); if (values != null) { if (values.isArray()) { if (index < values.size()) return values.get(index); return values.get(random.nextInt(values.size())); } else return values; } JsonNode enumType = Json7.get(schema, "_enumType"); if (enumType != null) { if (types.containsKey(enumType.asText())) { List<String> typeValues = types.get(enumType.asText()); String value = typeValues.get(random.nextInt(typeValues.size())); return Json7.toNode(value); } } JsonNode examples = Json7.get(schema, "_examples"); if (examples != null) { if (examples.isArray()) { if (index < examples.size()) return examples.get(index); return examples.get(random.nextInt(examples.size())); } else return examples; } String type = schema.get("_type").asText(); if ("text".equals(type)) return TextNode.valueOf( "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness."); else if ("string".equals(type)) return TextNode.valueOf("RD5654GH78"); else if ("boolean".equals(type)) return BooleanNode.valueOf(random.nextBoolean()); else if ("integer".equals(type)) return IntNode.valueOf(random.nextInt()); else if ("long".equals(type)) return LongNode.valueOf(random.nextLong()); else if ("float".equals(type)) return FloatNode.valueOf(random.nextFloat()); else if ("double".equals(type)) return DoubleNode.valueOf(random.nextDouble()); else if ("date".equals(type)) return TextNode.valueOf("2015-09-09"); else if ("time".equals(type)) return TextNode.valueOf("15:30:00"); else if ("timestamp".equals(type)) return TextNode.valueOf("2015-01-09T15:37:00.123Z"); else if ("enum".equals(type)) return TextNode.valueOf("blue"); else if ("geopoint".equals(type)) return Json7.object("lat", 48 + random.nextDouble(), "lon", 2 + random.nextDouble()); else if ("object".equals(type)) return generateObject(path, schema, index); return NullNode.getInstance(); }
public static GtExpression gt(String property, Float value) { return new GtExpression(val(property), FloatNode.valueOf(value)); }
public static LtExpression lt(String property, Float value) { return new LtExpression(val(property), FloatNode.valueOf(value)); }
public static GeExpression ge(String property, Float value) { return new GeExpression(val(property), FloatNode.valueOf(value)); }
public static LeExpression le(String property, Float value) { return new LeExpression(val(property), FloatNode.valueOf(value)); }
private Float convert(Float value) { return (Float) FloatConverter.converter().value(new FloatNode(value)); }
private static <T> void testTypeDecoding(Decoder.Type type, Map<T, byte[]> tests) throws IOException { NodeCache cache = new CHMCache(); for (Map.Entry<T, byte[]> entry : tests.entrySet()) { T expect = entry.getKey(); byte[] input = entry.getValue(); String desc = "decoded " + type.name() + " - " + expect; FileChannel fc = DecoderTest.getFileChannel(input); MappedByteBuffer mmap = fc.map(MapMode.READ_ONLY, 0, fc.size()); try { Decoder decoder = new Decoder(cache, mmap, 0); decoder.POINTER_TEST_HACK = true; // XXX - this could be streamlined if (type.equals(Decoder.Type.BYTES)) { assertArrayEquals(desc, (byte[]) expect, decoder.decode(0).binaryValue()); } else if (type.equals(Decoder.Type.ARRAY)) { assertEquals(desc, expect, decoder.decode(0)); } else if (type.equals(Decoder.Type.UINT16) || type.equals(Decoder.Type.INT32)) { assertEquals(desc, expect, decoder.decode(0).asInt()); } else if (type.equals(Decoder.Type.UINT32) || type.equals(Decoder.Type.POINTER)) { assertEquals(desc, expect, decoder.decode(0).asLong()); } else if (type.equals(Decoder.Type.UINT64) || type.equals(Decoder.Type.UINT128)) { assertEquals(desc, expect, decoder.decode(0).bigIntegerValue()); } else if (type.equals(Decoder.Type.DOUBLE)) { assertEquals(desc, expect, decoder.decode(0).asDouble()); } else if (type.equals(Decoder.Type.FLOAT)) { assertEquals(desc, new FloatNode((Float) expect), decoder.decode(0)); } else if (type.equals(Decoder.Type.UTF8_STRING)) { assertEquals(desc, expect, decoder.decode(0).asText()); } else if (type.equals(Decoder.Type.BOOLEAN)) { assertEquals(desc, expect, decoder.decode(0).asBoolean()); } else { assertEquals(desc, expect, decoder.decode(0)); } } finally { fc.close(); } } }