@Override public boolean decode(byte[] link, Collection<DependencyLink> out) { return JsonCodec.read(READER, link, out); }
@Override public boolean decodeList(byte[] links, Collection<DependencyLink> out) { return JsonCodec.readList(READER, links, out); }
@Override public byte[] encode(Span span) { return JsonCodec.write(writer, span); }
public static <T> byte[] writeList(Buffer.Writer<T> writer, List<T> value) { if (value.isEmpty()) return new byte[] {'[', ']'}; Buffer result = new Buffer(sizeInBytes(writer, value)); writeList(writer, value, result); return result.toByteArray(); }
@Override public int encodeList(List<Span> spans, byte[] out, int pos) { return JsonCodec.writeList(writer, spans, out, pos); } },
public static <T> boolean read(JsonReaderAdapter<T> adapter, byte[] bytes, Collection<T> out) { if (bytes.length == 0) return false; try { out.add(adapter.fromJson(new JsonReader(bytes))); return true; } catch (Exception e) { throw exceptionReading(adapter.toString(), e); } }
@Override @Nullable public DependencyLink decodeOne(byte[] link) { return JsonCodec.readOne(READER, link); }
@Override public byte[] encodeList(List<Span> spans) { return JsonCodec.writeList(writer, spans); }
public static <T> boolean readList( JsonReaderAdapter<T> adapter, byte[] bytes, Collection<T> out) { if (bytes.length == 0) return false; JsonReader reader = new JsonReader(bytes); try { reader.beginArray(); if (!reader.hasNext()) return false; while (reader.hasNext()) out.add(adapter.fromJson(reader)); reader.endArray(); return true; } catch (Exception e) { throw exceptionReading("List<" + adapter + ">", e); } }
@Override public Span decodeOne(byte[] span) { V1Span v1 = JsonCodec.readOne(new V1JsonSpanReader(), span); List<Span> out = new ArrayList<>(1); V1SpanConverter.create().convert(v1, out); return out.get(0); }
@Override public byte[] encodeList(List<DependencyLink> links) { return JsonCodec.writeList(WRITER, links); } };
public boolean readList(byte[] bytes, Collection<Span> out) { if (bytes.length == 0) return false; V1SpanConverter converter = V1SpanConverter.create(); JsonReader reader = new JsonReader(bytes); try { reader.beginArray(); if (!reader.hasNext()) return false; while (reader.hasNext()) { V1Span result = fromJson(reader); converter.convert(result, out); } reader.endArray(); return true; } catch (Exception e) { throw exceptionReading("List<Span>", e); } }
@Override @Nullable public Span decodeOne(byte[] span) { return JsonCodec.readOne(new V2SpanReader(), span); }
public static @Nullable <T> T readOne(JsonReaderAdapter<T> adapter, byte[] bytes) { List<T> out = new ArrayList<>(1); // TODO: could make single-element list w/o array if (!read(adapter, bytes, out)) return null; return out.get(0); }
@Override public byte[] encode(DependencyLink link) { return JsonCodec.write(WRITER, link); }
@Override public boolean decodeList(byte[] spans, Collection<Span> out) { // ex getTrace return JsonCodec.readList(new V2SpanReader(), spans, out); }
@Override public int encodeList(List<Span> spans, byte[] out, int pos) { return JsonCodec.writeList(new V1JsonSpanWriter(), spans, out, pos); } },
@Override public boolean decode(byte[] span, Collection<Span> out) { // ex decode span in dependencies job return JsonCodec.read(new V2SpanReader(), span, out); }
@Override public byte[] encode(Span span) { return JsonCodec.write(new V1JsonSpanWriter(), span); }
public static <T> int writeList(Buffer.Writer<T> writer, List<T> value, byte[] out, int pos) { if (value.isEmpty()) { out[pos++] = '['; out[pos++] = ']'; return 2; } int initialPos = pos; Buffer result = new Buffer(out, pos); writeList(writer, value, result); return result.pos - initialPos; }