/** * Content of buffer can be copied to another array of appropriate size. * This method can't be used when targeting output stream. * Ideally it should be avoided if possible, since it will create an array copy. * It's better to use getByteBuffer and size instead. * * @return copy of the buffer up to the current position */ public final byte[] toByteArray() { if (target != null) { throw new SerializationException("Method is not available when targeting stream"); } return Arrays.copyOf(buffer, position); }
@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); } }
/** * When JsonWriter does not target stream, this method should be used to copy content of the buffer into target stream. * It will also reset the buffer position to 0 so writer can be continued to be used even without a call to reset(). * * @param stream target stream * @throws IOException propagates from stream.write */ public final void toStream(final OutputStream stream) throws IOException { if (target != null) { throw new SerializationException("Method should not be used when targeting streams. Instead use flush() to copy what's remaining in the buffer"); } stream.write(buffer, 0, position); flushed += position; position = 0; }
private boolean checkSignatureNotFound() { int i = 0; while (i < 50) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new SerializationException(e); } if (resolved != null) { resolvedWriter = resolved; break; } i++; } return resolved == null; }
private boolean checkSignatureNotFound() { int i = 0; ImmutableDescription local = null; while (i < 50) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new SerializationException(e); } local = resolved; if (local != null) { encoder = local; decoder = local; break; } i++; } return local == null; }
/** * If stream was used, copies the buffer to stream and resets the position in buffer to 0. * It will not reset the stream as target, * meaning new usages of the JsonWriter will try to use the already provided stream. * It will not do anything if stream was not used * * To reset the stream to null use reset() or reset(OutputStream) methods. */ public final void flush() { if (target != null && position != 0) { try { target.write(buffer, 0, position); } catch (IOException ex) { throw new SerializationException("Unable to write to target stream.", ex); } flushed += position; position = 0; } }
private boolean checkSignatureNotFound() { int i = 0; ObjectFormatDescription local = null; while (i < 50) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new SerializationException(e); } local = resolved; if (local != null) { resolvedWriter = local; resolvedReader = local; resolvedBinder = local; break; } i++; } return local == null; }
private void enlargeOrFlush(final int size, final int padding) { if (target != null) { try { target.write(buffer, 0, size); } catch (IOException ex) { throw new SerializationException("Unable to write to target stream.", ex); } position = 0; flushed += size; if (padding > buffer.length) { buffer = Arrays.copyOf(buffer, buffer.length + buffer.length / 2 + padding); } } else { buffer = Arrays.copyOf(buffer, buffer.length + buffer.length / 2 + padding); } }
@Override public void accept(Object instance, @Nullable Object value) { try { field.set(instance, value); } catch (IllegalAccessException e) { throw new SerializationException("Unable to set field " + field.getName() + " of " + field.getDeclaringClass(), e); } } }
@Override public void accept(Object instance, @Nullable Object value) { try { method.invoke(instance, value); } catch (IllegalAccessException | InvocationTargetException e) { throw new SerializationException("Unable to call method " + method.getName() + " of " + method.getDeclaringClass(), e); } } }
@Override public Object apply(@Nullable Object instance) { try { return field.get(instance); } catch (IllegalAccessException e) { throw new SerializationException("Unable to read field " + field.getName() + " of " + field.getDeclaringClass(), e); } } }
@Override public Object apply(@Nullable Object instance) { try { return method.invoke(instance); } catch (IllegalAccessException | InvocationTargetException e) { throw new SerializationException("Unable to call method " + method.getName() + " of " + method.getDeclaringClass(), e); } } }
public static void serialize( final ResultSet rs, final JsonWriter buffer, @Nullable final OutputStream stream) throws SQLException, IOException { final ResultSetMetaData metadata = rs.getMetaData(); final int columns = metadata.getColumnCount(); if (columns == 0) throw new SerializationException("No columns found in ResultSet"); final Writer[] writers = new Writer[columns]; for (int i = 0; i < writers.length; i++) { final Writer wrt = writers[i] = createWriter(metadata, i + 1); if (wrt == null) throw new SerializationException("Unable to find Writer for column " + i); } serialize(rs, stream, buffer, writers); }
/** * Generic object serializer which is used for "unknown schema" objects. * It will throw SerializationException in case if it doesn't know how to serialize provided instance. * Will delegate the serialization to UnknownSerializer, which in most cases is the DslJson instance from which the writer was created. * This enables it to use DslJson configuration and serialize using custom serializers (when they are provided). * * @param value instance to serialize */ public void serializeObject(@Nullable final Object value) { if (value == null) { writeNull(); } else if (unknownSerializer != null) { try { unknownSerializer.serialize(this, value); } catch (IOException ex) { //serializing unknown stuff can fail in various ways ;( throw new SerializationException(ex); } } else { throw new SerializationException("Unable to serialize: " + value.getClass() + ".\n" + "Check that JsonWriter was created through DslJson#newWriter."); } } }
public static void serialize(@Nullable final Image value, final JsonWriter sw) { if (value == null) { sw.writeNull(); return; } final RenderedImage image; if (value instanceof RenderedImage) { image = (RenderedImage) value; } else { final BufferedImage bufferedImage = new BufferedImage(value.getWidth(null), value.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR); final Graphics bGr = bufferedImage.createGraphics(); bGr.drawImage(value, 0, 0, null); bGr.dispose(); image = bufferedImage; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { javax.imageio.ImageIO.write(image, "png", baos); BinaryConverter.serialize(baos.toByteArray(), sw); } catch (final IOException e) { throw new SerializationException(e); } }
@Override public void write(JsonWriter writer, @Nullable Optional<T> value) { if (value == null || !value.isPresent()) writer.writeNull(); else if (encoder != null) encoder.write(writer, value.get()); else { final T unpacked = value.get(); final JsonWriter.WriteObject jw = json.tryFindWriter(unpacked.getClass()); if (jw == null) { throw new SerializationException("Unable to find writer for " + unpacked.getClass()); } jw.write(writer, unpacked); } } }
@Override public void write(final JsonWriter writer, @Nullable final T instance) { if (instance == null) { writer.writeNull(); return; } final Class<?> current = instance.getClass(); for (FormatDescription<T> od : descriptions) { if (current != od.manifest) continue; if (od.isObjectFormatFirst) { od.objectFormat.write(writer, instance); } else { od.arrayFormat.write(writer, instance); } return; } throw new SerializationException("Unable to find encoder for '" + instance.getClass() + "' while encoding " + manifest.getTypeName() + ". Add @CompiledJson to specified type to allow serialization from it"); } }
@Override public Object read(final JsonReader reader) throws IOException { if (decoder == null) { if (checkSignatureNotFound()) { final JsonReader.ReadObject tmp = json.tryFindReader(type); if (tmp == null || tmp == this) { throw new SerializationException("Unable to find reader for " + type); } decoder = tmp; } } return decoder.read(reader); }
@Override public Object read(JsonReader reader) throws IOException { if (resolvedReader == null) { if (checkSignatureNotFound()) { final JsonReader.ReadObject tmp = json.tryFindReader(type); if (tmp == null || tmp == this) { throw new SerializationException("Unable to find reader for " + type); } resolvedReader = tmp; } } return resolvedReader.read(reader); }
@Override public Object bind(final JsonReader reader, final Object instance) throws IOException { if (resolvedBinder == null) { if (checkSignatureNotFound()) { final JsonReader.BindObject tmp = json.tryFindBinder(type); if (tmp == null || tmp == this) { throw new SerializationException("Unable to find binder for " + type); } resolvedBinder = tmp; } } return resolvedBinder.bind(reader, instance); }