com.dslplatform.json.JsonWriter Java Examples
The following examples show how to use
com.dslplatform.json.JsonWriter.
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: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 6 votes |
private void serializeGlobalLabels(ArrayList<String> globalLabelKeys, ArrayList<String> globalLabelValues) { if (!globalLabelKeys.isEmpty()) { writeFieldName("labels"); jw.writeByte(OBJECT_START); writeStringValue(sanitizeLabelKey(globalLabelKeys.get(0), replaceBuilder), replaceBuilder, jw); jw.writeByte(JsonWriter.SEMI); writeStringValue(globalLabelValues.get(0), replaceBuilder, jw); for (int i = 0; i < globalLabelKeys.size(); i++) { jw.writeByte(COMMA); writeStringValue(sanitizeLabelKey(globalLabelKeys.get(i), replaceBuilder), replaceBuilder, jw); jw.writeByte(JsonWriter.SEMI); writeStringValue(globalLabelValues.get(i), replaceBuilder, jw); } jw.writeByte(OBJECT_END); jw.writeByte(COMMA); } }
Example #2
Source File: ArrayFormatDescription.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 6 votes |
public ArrayFormatDescription( final Type manifest, final InstanceFactory<B> newInstance, final Settings.Function<B, T> finalize, final JsonWriter.WriteObject[] encoders, final JsonReader.BindObject[] decoders) { if (manifest == null) throw new IllegalArgumentException("manifest can't be null"); if (newInstance == null) throw new IllegalArgumentException("create can't be null"); if (finalize == null) throw new IllegalArgumentException("finalize can't be null"); if (encoders == null) throw new IllegalArgumentException("encoders can't be null or empty"); if (decoders == null) throw new IllegalArgumentException("decoders can't be null"); if (encoders.length != decoders.length) throw new IllegalArgumentException("decoders must match encoders (" + decoders.length + " != " + encoders.length + ")"); this.manifest = manifest; this.newInstance = newInstance; this.finalize = finalize; this.isEmpty = encoders.length == 0; this.encoders = encoders.clone(); this.decoders = decoders.clone(); this.startError = String.format("Expecting '[' to start decoding %s", Reflection.typeDescription(manifest)); this.endError = String.format("Expecting ']' to end decoding %s", Reflection.typeDescription(manifest)); this.countError = String.format("Expecting to read %d elements in the array while decoding %s", decoders.length, Reflection.typeDescription(manifest)); }
Example #3
Source File: ImmutableDescription.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 6 votes |
ImmutableDescription( final Type manifest, final Object[] defArgs, final Settings.Function<Object[], T> newInstance, final JsonWriter.WriteObject[] encoders, final DecodePropertyInfo<JsonReader.ReadObject>[] decoders, final boolean alwaysSerialize, final boolean skipOnUnknown) { super(encoders, alwaysSerialize); if (manifest == null) throw new IllegalArgumentException("manifest can't be null"); if (defArgs == null) throw new IllegalArgumentException("defArgs can't be null"); if (newInstance == null) throw new IllegalArgumentException("create can't be null"); if (decoders == null) throw new IllegalArgumentException("decoders can't be null"); this.manifest = manifest; this.defArgs = defArgs; this.newInstance = newInstance; this.decoders = DecodePropertyInfo.prepare(decoders); this.skipOnUnknown = skipOnUnknown; this.mandatoryFlag = DecodePropertyInfo.calculateMandatory(this.decoders); hasMandatory = mandatoryFlag != 0; this.startError = String.format("Expecting '{' to start decoding %s", Reflection.typeDescription(manifest)); this.endError = String.format("Expecting '}' or ',' while decoding %s", Reflection.typeDescription(manifest)); }
Example #4
Source File: MetricRegistrySerializer.java From apm-agent-java with Apache License 2.0 | 6 votes |
static void serializeMetricSet(MetricSet metricSet, long epochMicros, StringBuilder replaceBuilder, JsonWriter jw) { jw.writeByte(JsonWriter.OBJECT_START); { DslJsonSerializer.writeFieldName("metricset", jw); jw.writeByte(JsonWriter.OBJECT_START); { DslJsonSerializer.writeFieldName("timestamp", jw); NumberConverter.serialize(epochMicros, jw); jw.writeByte(JsonWriter.COMMA); DslJsonSerializer.serializeLabels(metricSet.getLabels(), replaceBuilder, jw); DslJsonSerializer.writeFieldName("samples", jw); jw.writeByte(JsonWriter.OBJECT_START); boolean hasSamples = serializeGauges(metricSet.getGauges(), jw); hasSamples |= serializeTimers(metricSet.getTimers(), hasSamples, jw); serializeCounters(metricSet.getCounters(), hasSamples, jw); jw.writeByte(JsonWriter.OBJECT_END); } jw.writeByte(JsonWriter.OBJECT_END); } jw.writeByte(JsonWriter.OBJECT_END); }
Example #5
Source File: DslJsonbProvider.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void toJson(Object obj, Type type, OutputStream stream) throws JsonbException { if (type == null) throw new JsonbException("type can't be null"); if (stream == null) throw new JsonbException("stream can't be null"); JsonWriter jw = localWriter.get(); try { jw.reset(stream); if (!dslJson.serialize(jw, type, obj)) { throw new JsonbException("Unable to serialize provided " + type); } jw.flush(); } catch (SerializationException ex) { throw new JsonbException(ex.getMessage(), ex.getCause()); } finally { jw.reset(null); } }
Example #6
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 6 votes |
private void recursiveSerializeException(@Nullable Throwable exception) { jw.writeByte(JsonWriter.OBJECT_START); if (exception != null) { writeField("message", String.valueOf(exception.getMessage())); serializeStacktrace(exception.getStackTrace()); writeFieldName("type"); writeStringValue(exception.getClass().getName()); Throwable cause = exception.getCause(); if (cause != null) { jw.writeByte(COMMA); writeFieldName("cause"); jw.writeByte(ARRAY_START); recursiveSerializeException(cause); jw.writeByte(ARRAY_END); } } jw.writeByte(JsonWriter.OBJECT_END); }
Example #7
Source File: BValueJsonCodec.java From gridgo with MIT License | 6 votes |
@Override public void write(JsonWriter writer, BValue value) { if (value == null || value.isNull()) { writer.writeNull(); return; } switch (value.getType()) { case CHAR: writer.writeString(value.getString()); break; case RAW: writer.writeString(ByteArrayUtils.toHex(value.getRaw(), "0x")); break; default: writer.serializeObject(value.getData()); break; } }
Example #8
Source File: BuilderTest.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 6 votes |
public BuilderTest() { ObjectFormatDescription<Immutable.Builder, Immutable> description = new ObjectFormatDescription<Immutable.Builder, Immutable>( Immutable.class, Immutable.Builder::new, Immutable.Builder::build, new JsonWriter.WriteObject[] { Settings.<Immutable, Integer>createEncoder(c -> c.x, "x", json, int.class), Settings.<Immutable, Long>createEncoder(c -> c.y, "y", json, long.class) }, new DecodePropertyInfo[] { Settings.<Immutable.Builder, Integer>createDecoder(Immutable.Builder::x, "x", json, int.class), Settings.<Immutable.Builder, Long>createDecoder(Immutable.Builder::y, "y", json, long.class) }, json, true ); json.registerReader(Immutable.class, description); json.registerWriter(Immutable.class, description); }
Example #9
Source File: BElementJsonCodec.java From gridgo with MIT License | 6 votes |
@Override public void write(JsonWriter writer, BElement value) { if (value == null) writer.writeNull(); if (value.isObject()) objectCodec.write(writer, value.asObject()); if (value.isArray()) arrayCodec.write(writer, value.asArray()); if (value.isValue()) valueCodec.write(writer, value.asValue()); if (value.isReference()) referenceCodec.write(writer, value.asReference()); }
Example #10
Source File: DslJsonSerializerTest.java From apm-agent-java with Apache License 2.0 | 6 votes |
@Test void testLimitStringValueLength() throws IOException { StringBuilder longValue = new StringBuilder(DslJsonSerializer.MAX_VALUE_LENGTH + 1); for (int i = 0; i < DslJsonSerializer.MAX_VALUE_LENGTH + 1; i++) { longValue.append('0'); } StringBuilder longStringValue = new StringBuilder(DslJsonSerializer.MAX_LONG_STRING_VALUE_LENGTH + 1); for (int i = 0; i < DslJsonSerializer.MAX_LONG_STRING_VALUE_LENGTH + 1; i++) { longStringValue.append('0'); } serializer.jw.writeByte(JsonWriter.OBJECT_START); serializer.writeField("string", longValue.toString()); serializer.writeField("stringBuilder", longValue); serializer.writeLongStringField("longString", longStringValue.toString()); serializer.writeLastField("lastString", longValue.toString()); serializer.jw.writeByte(JsonWriter.OBJECT_END); final JsonNode jsonNode = objectMapper.readTree(serializer.jw.toString()); assertThat(jsonNode.get("stringBuilder").textValue()).hasSize(DslJsonSerializer.MAX_VALUE_LENGTH).endsWith("…"); assertThat(jsonNode.get("string").textValue()).hasSize(DslJsonSerializer.MAX_VALUE_LENGTH).endsWith("…"); assertThat(jsonNode.get("longString").textValue()).hasSize(DslJsonSerializer.MAX_LONG_STRING_VALUE_LENGTH).endsWith("…"); assertThat(jsonNode.get("lastString").textValue()).hasSize(DslJsonSerializer.MAX_VALUE_LENGTH).endsWith("…"); }
Example #11
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 6 votes |
private static void serializeStringKeyScalarValueMap(Iterator<? extends Map.Entry<String, ? /* String|Number|Boolean */>> it, StringBuilder replaceBuilder, JsonWriter jw, boolean extendedStringLimit, boolean supportsNonStringValues) { jw.writeByte(OBJECT_START); if (it.hasNext()) { Map.Entry<String, ?> kv = it.next(); writeStringValue(sanitizeLabelKey(kv.getKey(), replaceBuilder), replaceBuilder, jw); jw.writeByte(JsonWriter.SEMI); serializeScalarValue(replaceBuilder, jw, kv.getValue(), extendedStringLimit, supportsNonStringValues); while (it.hasNext()) { jw.writeByte(COMMA); kv = it.next(); writeStringValue(sanitizeLabelKey(kv.getKey(), replaceBuilder), replaceBuilder, jw); jw.writeByte(JsonWriter.SEMI); serializeScalarValue(replaceBuilder, jw, kv.getValue(), extendedStringLimit, supportsNonStringValues); } } jw.writeByte(OBJECT_END); }
Example #12
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 6 votes |
private static void serializeScalarValue(StringBuilder replaceBuilder, JsonWriter jw, Object value, boolean extendedStringLimit, boolean supportsNonStringValues) { if (value instanceof String) { if (extendedStringLimit) { writeLongStringValue((String) value, replaceBuilder, jw); } else { writeStringValue((String) value, replaceBuilder, jw); } } else if (value instanceof Number) { if (supportsNonStringValues) { NumberConverter.serialize(((Number) value).doubleValue(), jw); } else { jw.writeNull(); } } else if (value instanceof Boolean) { if (supportsNonStringValues) { BoolConverter.serialize((Boolean) value, jw); } else { jw.writeNull(); } } else { // can't happen, as AbstractContext enforces the values to be either String, Number or boolean jw.writeString("invalid value"); } }
Example #13
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 6 votes |
private void serializePotentiallyMultiValuedEntry(String key, @Nullable Object value) { jw.writeString(key); jw.writeByte(JsonWriter.SEMI); if (value instanceof String) { StringConverter.serialize((String) value, jw); } else if (value instanceof List) { jw.writeByte(ARRAY_START); final List<String> values = (List<String>) value; jw.writeString(values.get(0)); for (int i = 1; i < values.size(); i++) { jw.writeByte(COMMA); jw.writeString(values.get(i)); } jw.writeByte(ARRAY_END); } else if (value == null) { jw.writeNull(); } }
Example #14
Source File: World.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void serialize(JsonWriter writer, boolean minimal) { writer.writeAscii("{\"id\":"); NumberConverter.serialize(this.id, writer); writer.writeAscii(",\"randomNumber\":"); NumberConverter.serialize(this.randomNumber, writer); writer.writeByte(com.dslplatform.json.JsonWriter.OBJECT_END); }
Example #15
Source File: WriteDescription.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 5 votes |
public final void writeContentFull(final JsonWriter writer, @Nullable final T instance) { if (isEmpty) return; encoders[0].write(writer, instance); for (int i = 1; i < encoders.length; i++) { writer.writeByte(JsonWriter.COMMA); encoders[i].write(writer, instance); } }
Example #16
Source File: DslJsonbProvider.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public String toJson(Object obj, Type type) throws JsonbException { if (type == null) throw new JsonbException("type can't be null"); try { JsonWriter writer = localWriter.get(); writer.reset(); if (!dslJson.serialize(writer, type, obj)) { throw new JsonbException("Unable to serialize provided " + type); } return new String(writer.getByteBuffer(), 0, writer.size(), "UTF-8"); } catch (IOException | SerializationException ex) { throw new JsonbException(ex.getMessage(), ex.getCause()); } }
Example #17
Source File: DslJsonbProvider.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public String toJson(Object obj) throws JsonbException { try { JsonWriter writer = localWriter.get(); writer.reset(); dslJson.serialize(writer, obj); return new String(writer.getByteBuffer(), 0, writer.size(), "UTF-8"); } catch (IOException | SerializationException ex) { throw new JsonbException(ex.getMessage(), ex.getCause()); } }
Example #18
Source File: DslJsonbProvider.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void toJson(Object obj, Writer writer) throws JsonbException { if (writer == null) throw new JsonbException("writer can't be null"); try { JsonWriter jw = localWriter.get(); jw.reset(); dslJson.serialize(jw, obj); //TODO: not ideal... but lets use it instead of throwing an exception writer.write(new String(jw.getByteBuffer(), 0, jw.size(), "UTF-8")); } catch (IOException | SerializationException ex) { throw new JsonbException(ex.getMessage(), ex.getCause()); } }
Example #19
Source File: HexUtils.java From apm-agent-java with Apache License 2.0 | 5 votes |
public static void writeAsHex(long l, JsonWriter jw) { writeHexByte(jw, (byte) (l >> 56)); writeHexByte(jw, (byte) (l >> 48)); writeHexByte(jw, (byte) (l >> 40)); writeHexByte(jw, (byte) (l >> 32)); writeHexByte(jw, (byte) (l >> 24)); writeHexByte(jw, (byte) (l >> 16)); writeHexByte(jw, (byte) (l >> 8)); writeHexByte(jw, (byte) l); }
Example #20
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
private void writeHexField(String fieldName, Id traceId) { writeFieldName(fieldName); jw.writeByte(JsonWriter.QUOTE); traceId.writeAsHex(jw); jw.writeByte(JsonWriter.QUOTE); jw.writeByte(COMMA); }
Example #21
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
static void writeLastField(final String fieldName, @Nullable final CharSequence value, StringBuilder replaceBuilder, final JsonWriter jw) { writeFieldName(fieldName, jw); if (value != null && value.length() > 0) { writeStringValue(value, replaceBuilder, jw); } else { jw.writeNull(); } }
Example #22
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
private static void writeLongStringValue(CharSequence value, StringBuilder replaceBuilder, JsonWriter jw) { if (value.length() > MAX_LONG_STRING_VALUE_LENGTH) { replaceBuilder.setLength(0); replaceBuilder.append(value, 0, Math.min(value.length(), MAX_LONG_STRING_VALUE_LENGTH + 1)); writeLongStringBuilderValue(replaceBuilder, jw); } else { jw.writeString(value); } }
Example #23
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
private static void writeLongStringBuilderValue(StringBuilder value, JsonWriter jw) { if (value.length() > MAX_LONG_STRING_VALUE_LENGTH) { value.setLength(MAX_LONG_STRING_VALUE_LENGTH - 1); value.append('…'); } jw.writeString(value); }
Example #24
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
private static void writeStringValue(CharSequence value, StringBuilder replaceBuilder, JsonWriter jw) { if (value.length() > MAX_VALUE_LENGTH) { replaceBuilder.setLength(0); replaceBuilder.append(value, 0, Math.min(value.length(), MAX_VALUE_LENGTH + 1)); writeStringBuilderValue(replaceBuilder, jw); } else { jw.writeString(value); } }
Example #25
Source File: AttributeObjectAlwaysEncoder.java From dsl-json with BSD 3-Clause "New" or "Revised" License | 5 votes |
AttributeObjectAlwaysEncoder( final Settings.Function<T, R> read, final String name, final JsonWriter.WriteObject<R> encoder) { if (read == null) throw new IllegalArgumentException("read can't be null"); if (name == null || name.isEmpty()) throw new IllegalArgumentException("name can't be null"); if (encoder == null) throw new IllegalArgumentException("encoder can't be null"); this.read = read; quotedName = ("\"" + name + "\":").getBytes(utf8); this.encoder = encoder; }
Example #26
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
private static void writeStringBuilderValue(StringBuilder value, JsonWriter jw) { if (value.length() > MAX_VALUE_LENGTH) { value.setLength(MAX_VALUE_LENGTH - 1); value.append('…'); } jw.writeString(value); }
Example #27
Source File: DslJsonSerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
static void writeField(final String fieldName, @Nullable final CharSequence value, StringBuilder replaceBuilder, JsonWriter jw) { if (value != null) { writeFieldName(fieldName, jw); writeStringValue(value, replaceBuilder, jw); jw.writeByte(COMMA); } }
Example #28
Source File: MetricRegistrySerializer.java From apm-agent-java with Apache License 2.0 | 5 votes |
public static void serialize(Map<? extends Labels, MetricSet> metricSets, StringBuilder replaceBuilder, JsonWriter jw) { final long timestamp = System.currentTimeMillis() * 1000; for (MetricSet metricSet : metricSets.values()) { if (metricSet.hasContent()) { serializeMetricSet(metricSet, timestamp, replaceBuilder, jw); metricSet.onAfterReport(); jw.writeByte(NEW_LINE); } } }
Example #29
Source File: JsonServlet.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("application/json"); final Message msg = new Message("Hello, World!"); final JsonWriter writer = Utils.getJson(); msg.serialize(writer, false); writer.toStream(res.getOutputStream()); }
Example #30
Source File: HexUtilsTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test void testLongToHex() { byte[] bytes = new byte[8]; HexUtils.nextBytes("09c2572177fdae24", 0, bytes); long l = ByteUtils.getLong(bytes, 0); JsonWriter jw = new DslJson<>().newWriter(); HexUtils.writeAsHex(l, jw); assertThat(jw.toString()).isEqualTo("09c2572177fdae24"); }