@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
@Override public void encode(org.bson.BsonWriter writer, T value, EncoderContext encoderContext) { try { adapter.write(new BsonWriter(writer), value); } catch (IOException e) { throw new RuntimeException(String.format("Couldn't write value of class %s: %s", clazz.getName(), value), e); } }
@Override public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); return; } for (int i = 0; i < subtypes.length; i++) { TypeToken<? extends T> subtype = subtypes[i]; if (subtype.getRawType().isInstance(value)) { // safe unchecked, type is checked at runtime @SuppressWarnings("unchecked") TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) adapters.get(i); typeAdapter.write(out, value); return; } } gson.toJson(value, value.getClass(), out); }
@Override public void write(JsonWriter out, Binary value) throws IOException { if (out instanceof BsonWriter) { BINARY_ADAPTER.write(out, value.value()); } else { out.value(value.toString()); } }
@Override public void write(JsonWriter out, TimeInstant value) throws IOException { if (out instanceof BsonWriter) { TIME_INSTANT_ADAPTER.write(out, value.value()); } else { out.value(value.toString()); } }
@Override public void write(JsonWriter out, Id value) throws IOException { if (out instanceof BsonWriter) { OBJECT_ID_ADAPTER.write(out, value.value()); } else { out.value(value.toString()); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
.getImplementation(); delegate.write(out, unproxiedValue);
@SuppressWarnings("unchecked") @Override public void write(Gson gson, Type type, Object object, OutputStream stream) throws IOException { @Nullable JsonWriter writer = null; boolean wasOriginalException = false; try { writer = new JsonWriter(new BufferedWriter(new OutputStreamWriter(stream, CHARSET_NAME))); options.setWriterOptions(writer); gson.getAdapter((TypeToken<Object>) TypeToken.get(type)).write(writer, object); } catch (IOException ex) { wasOriginalException = true; throw ex; } catch (Exception ex) { wasOriginalException = true; throw new IOException(ex); } finally { if (writer != null) { try { // underlying stream should not be closed, just flushing writer.flush(); } catch (IOException ex) { if (!wasOriginalException) { throw ex; } } } } }
options.setWriterOptions(writer); gson.getAdapter((TypeToken<Object>) TypeToken.get(type)).write(writer, object); } catch (IOException ex) { wasOriginalException = true;
static org.bson.BsonReader asBsonReader(JsonObject gson) throws IOException { BasicOutputBuffer buffer = new BasicOutputBuffer(); BsonWriter writer = new BsonWriter(new BsonBinaryWriter(buffer)); TypeAdapters.JSON_ELEMENT.write(writer, gson); return new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray())); }
/** * Writes the JSON element to the writer, recursively. */ public static void write(JsonElement element, JsonWriter writer) throws IOException { TypeAdapters.JSON_ELEMENT.write(writer, element); }
@Override public void write(JsonWriter out, T value) throws IOException { if (delegate == null) { throw new IllegalStateException(); } delegate.write(out, value); } }
@Override public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { TypeAdapter.this.write(out, value); } } @Override public T read(JsonReader reader) throws IOException {
@Test public void dateCodec() throws IOException { TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec()); Date date = new Date(); BsonDocument doc = new BsonDocument(); BsonDocumentWriter writer = new BsonDocumentWriter(doc); writer.writeStartDocument(); writer.writeName("$date"); adapter.write(new BsonWriter(writer), date); writer.writeEndDocument(); check(doc.keySet()).hasSize(1); check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME); check(doc.get("$date").asDateTime().getValue()).is(date.getTime()); } }
@Override public void write(JsonWriter out, Collection<E> collection) throws IOException { if (collection == null) { out.nullValue(); return; } out.beginArray(); for (E element : collection) { elementTypeAdapter.write(out, element); } out.endArray(); } }
TypeAdapters.JSON_ELEMENT.write(new BsonWriter(new BsonDocumentWriter(doc)), obj);
@Override public void write(JsonWriter out, T value) throws IOException { if (serializer == null) { delegate().write(out, value); return; } if (value == null) { out.nullValue(); return; } JsonElement tree = serializer.serialize(value, typeToken.getType(), context); Streams.write(tree, out); }
@Override public void write(JsonWriter out, AtomicLongArray value) throws IOException { out.beginArray(); for (int i = 0, length = value.length(); i < length; i++) { longAdapter.write(out, value.get(i)); } out.endArray(); } @Override public AtomicLongArray read(JsonReader in) throws IOException {