/** * Creates a new ObjectWriter with this Genson instance configuration. */ public ObjectWriter createWriter(OutputStream os, Charset charset) { return createWriter(new OutputStreamWriter(os, charset)); }
/** * Creates a new ObjectWriter with this Genson instance configuration. */ public ObjectWriter createWriter(OutputStream os, Charset charset) { return createWriter(new OutputStreamWriter(os, charset)); }
/** * Serializes this object to its json form in a byte array. */ public byte[] serializeBytes(Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectWriter objectWriter = createWriter(baos); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); return baos.toByteArray(); }
/** * Serializes the object using the specified BeanViews. * * @param object * @param withViews the BeanViews to apply during this serialization. * @return the json string representing this object * @throws com.owlike.genson.JsonBindingException * @throws com.owlike.genson.stream.JsonStreamException * @see BeanView */ public String serialize(Object object, Class<? extends BeanView<?>> firstView, Class<? extends BeanView<?>>... withViews) { StringWriter sw = new StringWriter(); ObjectWriter writer = createWriter(sw); List<Class<? extends BeanView<?>>> views = new ArrayList(withViews.length); for (Class<? extends BeanView<?>> view : withViews) views.add(view); views.add(firstView); if (object == null) serializeNull(writer); else serialize(object, object.getClass(), writer, new Context(this, views)); return sw.toString(); }
/** * Serializes this object to its json form in a byte array. */ public byte[] serializeBytes(Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectWriter objectWriter = createWriter(baos); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); return baos.toByteArray(); }
/** * Serializes the object using the specified BeanViews. * * @param object * @param withViews the BeanViews to apply during this serialization. * @return the json string representing this object * @throws com.owlike.genson.JsonBindingException * @throws com.owlike.genson.stream.JsonStreamException * @see BeanView */ public String serialize(Object object, Class<? extends BeanView<?>> firstView, Class<? extends BeanView<?>>... withViews) { StringWriter sw = new StringWriter(); ObjectWriter writer = createWriter(sw); List<Class<? extends BeanView<?>>> views = new ArrayList(withViews.length); for (Class<? extends BeanView<?>> view : withViews) views.add(view); views.add(firstView); if (object == null) serializeNull(writer); else serialize(object, object.getClass(), writer, new Context(this, views)); return sw.toString(); }
/** * Serializes the object into a json string. * * @param object object to be serialized. * @return the serialized object as a string. * @throws com.owlike.genson.JsonBindingException if there was any kind of error during serialization. * @throws JsonStreamException if there was a problem during writing of the object to the output. */ public String serialize(Object object) { StringWriter sw = new StringWriter(); ObjectWriter writer = createWriter(sw); if (object == null) serializeNull(writer); else serialize(object, object.getClass(), writer, new Context(this)); return sw.toString(); }
/** * Serializes this object to the passed Writer, as Genson did not instantiate it, you are * responsible of calling close on it. */ public void serialize(Object object, Writer writer) { ObjectWriter objectWriter = createWriter(writer); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); }
/** * Serializes this object to the passed OutputStream, as Genson did not instantiate it, you are * responsible of calling close on it. */ public void serialize(Object object, OutputStream output) { ObjectWriter objectWriter = createWriter(output); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); }
/** * Serializes the object into a json string. * * @param object object to be serialized. * @return the serialized object as a string. * @throws com.owlike.genson.JsonBindingException if there was any kind of error during serialization. * @throws JsonStreamException if there was a problem during writing of the object to the output. */ public String serialize(Object object) { StringWriter sw = new StringWriter(); ObjectWriter writer = createWriter(sw); if (object == null) serializeNull(writer); else serialize(object, object.getClass(), writer, new Context(this)); return sw.toString(); }
/** * Serializes this object to the passed Writer, as Genson did not instantiate it, you are * responsible of calling close on it. */ public void serialize(Object object, Writer writer) { ObjectWriter objectWriter = createWriter(writer); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); }
/** * Serializes this object to the passed OutputStream, as Genson did not instantiate it, you are * responsible of calling close on it. */ public void serialize(Object object, OutputStream output) { ObjectWriter objectWriter = createWriter(output); if (object == null) serializeNull(objectWriter); else serialize(object, object.getClass(), objectWriter, new Context(this)); }
public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { Genson genson = getInstance(type); String charset = mediaType.getParameters().get("charset"); if (charset == null) charset = "UTF-8"; if (!charset.equalsIgnoreCase("UTF-8") && !charset.equalsIgnoreCase("UTF-16BE") && !charset.equalsIgnoreCase("UTF-16LE") && !charset.equalsIgnoreCase("UTF-32BE") && !charset.equalsIgnoreCase("UTF-32LE")) throw new UnsupportedEncodingException("JSON spec allows only UTF-8/16/32 encodings."); ObjectWriter writer = genson.createWriter(new OutputStreamWriter(entityStream, charset)); try { genson.serialize(t, rawIfNullGenericType(type, genericType), writer, createContext(annotations, genson)); writer.flush(); } catch (JsonBindingException e) { throw new WebApplicationException(e); } catch (JsonStreamException jse) { throw new WebApplicationException(jse); } }
public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { Genson genson = getInstance(type); String charset = mediaType.getParameters().get("charset"); if (charset == null) charset = "UTF-8"; if (!charset.equalsIgnoreCase("UTF-8") && !charset.equalsIgnoreCase("UTF-16BE") && !charset.equalsIgnoreCase("UTF-16LE") && !charset.equalsIgnoreCase("UTF-32BE") && !charset.equalsIgnoreCase("UTF-32LE")) throw new UnsupportedEncodingException("JSON spec allows only UTF-8/16/32 encodings."); ObjectWriter writer = genson.createWriter(new OutputStreamWriter(entityStream, charset)); try { genson.serialize(t, rawIfNullGenericType(type, genericType), writer, createContext(annotations, genson)); writer.flush(); } catch (JsonBindingException e) { throw new WebApplicationException(e); } catch (JsonStreamException jse) { throw new WebApplicationException(jse); } }
/** * Serializes the object using the type of GenericType instead of using its runtime type. * * @param object object to be serialized. * @param type the type of the object to be serialized. * @return json string representation. * @throws com.owlike.genson.JsonBindingException * @throws JsonStreamException */ public String serialize(Object object, GenericType<?> type) { StringWriter sw = new StringWriter(); ObjectWriter writer = createWriter(sw); if (object == null) serializeNull(writer); else serialize(object, type.getType(), writer, new Context(this)); return sw.toString(); }
/** * Serializes the object using the type of GenericType instead of using its runtime type. * * @param object object to be serialized. * @param type the type of the object to be serialized. * @return json string representation. * @throws com.owlike.genson.JsonBindingException * @throws JsonStreamException */ public String serialize(Object object, GenericType<?> type) { StringWriter sw = new StringWriter(); ObjectWriter writer = createWriter(sw); if (object == null) serializeNull(writer); else serialize(object, type.getType(), writer, new Context(this)); return sw.toString(); }
@SuppressWarnings("unchecked") @Override protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { ObjectWriter writer = genson.createWriter(outputMessage.getBody()); MethodParameter mp = ThreadLocalHolder.get("__GENSON$return_param", MethodParameter.class); WithBeanView ann = mp != null ? mp.getMethodAnnotation(WithBeanView.class) : null; if (ann != null) genson.serialize(t, writer, new Context(genson, Arrays.asList(ann.views()))); else genson.serialize(t, writer, new Context(genson)); writer.flush(); } }
@SuppressWarnings("unchecked") @Override protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { ObjectWriter writer = genson.createWriter(outputMessage.getBody()); MethodParameter mp = ThreadLocalHolder.get("__GENSON$return_param", MethodParameter.class); WithBeanView ann = mp != null ? mp.getMethodAnnotation(WithBeanView.class) : null; if (ann != null) genson.serialize(t, writer, new Context(genson, Arrays.asList(ann.views()))); else genson.serialize(t, writer, new Context(genson)); writer.flush(); } }
@Benchmark @Override public Object genson() throws Exception { ByteArrayOutputStream os = JsonUtils.byteArrayOutputStream(); ObjectWriter ow = JSON_SOURCE().provider().genson().createWriter(os); JSON_SOURCE().streamSerializer().genson(ow, JSON_SOURCE().nextPojo()); ow.close(); return os; }