@Override public void serialize(Book book, JsonGenerator generator, SerializationContext ctx) { if (book != null) { generator.writeStartObject(); ctx.serialize(book.getClass().getName(), book, generator); generator.writeEnd(); } else { ctx.serialize(null, generator); } }
@Override public void serialize(Magazine magazine, JsonGenerator generator, SerializationContext ctx) { if (magazine != null) { generator.writeStartObject(); ctx.serialize("name", magazine.getAuthor(), generator); generator.writeEnd(); } else { ctx.serialize(null, generator); } }
@Override public void serialize(Book book, JsonGenerator generator, SerializationContext ctx) { generator.writeStartObject(); generator.write("id", "QWE-123-RTS"); generator.write("title", book.getTitle()); generator.write("firstName", book.getAuthor().split(" ")[0]); generator.write("lastName", book.getAuthor().split(" ")[1]); generator.writeEnd(); }
@Override public void serialize(final CoffeeEvent event, final JsonGenerator generator, final SerializationContext ctx) { generator.write("class", event.getClass().getCanonicalName()); generator.writeStartObject("data"); ctx.serialize("data", event, generator); generator.writeEnd(); }
protected void serializeItem(Object item, JsonGenerator generator, SerializationContext ctx, JsonBindingModel model) { if (item == null) { generator.writeNull(); return; } Class<?> itemClass = item.getClass(); JsonbSerializer<?> serializer = getValueSerializer(itemClass); if (serializer == null) { serializer = new SerializerBuilder(((Marshaller)ctx).getJsonbContext()).withObjectClass(itemClass).withWrapper(this).withModel(model).build(); addValueSerializer(serializer, itemClass); } serializerCaptor(serializer, item, generator, ctx); }
@Override protected void serializeInternal(T object, JsonGenerator generator, SerializationContext ctx) { final PropertyModel[] allProperties = ((Marshaller) ctx).getMappingContext().getOrCreateClassModel(object.getClass()).getSortedProperties(); for (PropertyModel model : allProperties) { marshallProperty(object, generator, ctx, model); } }
@SuppressWarnings("unchecked") private void marshallProperty(T object, JsonGenerator generator, SerializationContext ctx, PropertyModel propertyModel) { Marshaller marshaller = (Marshaller) ctx; if (propertyModel.isReadable()) { final Object propertyValue = propertyModel.getValue(object); if (propertyValue == null || isEmptyOptional(propertyValue)) { if (propertyModel.getCustomization().isNillable()) { generator.writeNull(propertyModel.getWriteName()); } return; } generator.writeKey(propertyModel.getWriteName()); final JsonbSerializer<?> propertyCachedSerializer = propertyModel.getPropertySerializer(); if (propertyCachedSerializer != null) { serializerCaptor(propertyCachedSerializer, propertyValue, generator, ctx); return; } Optional<Type> runtimeTypeOptional = ReflectionUtils.resolveOptionalType(this, propertyModel.getType()); Type genericType = runtimeTypeOptional.orElse(null); final JsonbSerializer<?> serializer = new SerializerBuilder(marshaller.getJsonbContext()) .withWrapper(this) .withObjectClass(propertyValue.getClass()).withModel(propertyModel) .withType(genericType).build(); serializerCaptor(serializer, propertyValue, generator, ctx); } }
@SuppressWarnings("unchecked") @Override protected void serializeInternal(T obj, JsonGenerator generator, SerializationContext ctx) { for (Map.Entry<?,?> entry : obj.entrySet()) { final String keysString = String.valueOf(entry.getKey()); final Object value = entry.getValue(); if (value == null) { generator.writeNull(keysString); return; } generator.writeKey(keysString); serializeItem(value, generator, ctx, containerModel); } }
@Override protected void serializeInternal(float[] arr, JsonGenerator generator, SerializationContext ctx) { for (float obj : arr) { //floats lose precision, after upcasting to doubles in jsonp generator.write(new BigDecimal(String.valueOf(obj))); } }
@Override @SuppressWarnings("unchecked") public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { JsonbContext jsonbContext = ((Marshaller) ctx).getJsonbContext(); try { jsonbContext.addProcessedType(adapterInfo.getBindingType()); final JsonbAdapter<T, A> adapter = (JsonbAdapter<T, A>) adapterInfo.getAdapter(); A adapted = adapter.adaptToJson(obj); final JsonbSerializer<A> serializer = resolveSerializer((Marshaller) ctx, adapted); serializer.serialize(adapted, generator, ctx); } catch (Exception e) { throw new JsonbException(Messages.getMessage(MessageKeys.ADAPTER_EXCEPTION, adapterInfo.getBindingType(), adapterInfo.getToType(), adapterInfo.getAdapter().getClass()), e); } finally { jsonbContext.removeProcessedType(adapterInfo.getBindingType()); } }
@Override public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { JsonbContext jsonbContext = ((ProcessingContext) ctx).getJsonbContext(); if (obj == null || !obj.isPresent()) { if (!wrapperModel.getCustomization().isNillable()) { return; } generator.writeNull(); return; } Object optionalValue = obj.get(); final JsonbSerializer<?> serializer = new SerializerBuilder(jsonbContext).withObjectClass(optionalValue.getClass()) .withType(optionalValueType).withWrapper(wrapper).withModel(wrapperModel).build(); serialCaptor(serializer, optionalValue, generator, ctx); }
@Override public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { JsonbContext jsonbContext = ((Marshaller) ctx).getJsonbContext(); try { jsonbContext.addProcessedType(model.getType()); userSerializer.serialize(obj, generator, ctx); } finally { jsonbContext.removeProcessedType(model.getType()); } }
@Override public void serialize(SimpleContainer[] containers, JsonGenerator jsonGenerator, SerializationContext serializationContext) { jsonGenerator.writeStartArray(); for (SimpleContainer container : containers) { serializationContext.serialize(container, jsonGenerator); } jsonGenerator.writeEnd(); }
@Override public void serialize(Crate obj, JsonGenerator generator, SerializationContext ctx) { generator.writeStartObject(); generator.write("crateStr", "REPLACED crate str"); ctx.serialize("crateInner", obj.crateInner, generator); ctx.serialize("crateInnerList", obj.crateInnerList, generator); generator.write("crateBigDec", new BigDecimal("54321")); generator.writeEnd(); }
@Override public void serialize(Crate obj, JsonGenerator generator, SerializationContext ctx) { generator.writeStartObject(); generator.write("crateStr", "REPLACED crate str"); ctx.serialize("crateInner", obj.crateInner, generator); ctx.serialize("crateInnerList", obj.crateInnerList, generator); generator.write("crateBigDec", new BigDecimal("54321")); ctx.serialize("date-converted", obj.date, generator); generator.writeEnd(); }
@Override public final void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { writeStart(generator); serializeInternal(obj, generator, ctx); writeEnd(generator); }
@SuppressWarnings("unchecked") protected <X> void serializerCaptor(JsonbSerializer<?> serializer, X object, JsonGenerator generator, SerializationContext ctx) { ((JsonbSerializer<X>) serializer).serialize(object, generator, ctx); }
@Override public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { final JsonbContext jsonbContext = ((Marshaller) ctx).getJsonbContext(); final JsonbDateFormatter formatter = getJsonbDateFormatter(jsonbContext); generator.write(toJson(obj, formatter, jsonbContext)); }
@Override protected void serializeInternal(long[] arr, JsonGenerator generator, SerializationContext ctx) { for (long obj : arr) { generator.write(obj); } }
@Override protected void serializeInternal(T[] arr, JsonGenerator generator, SerializationContext ctx) { for (T obj : arr) { serializeItem(obj, generator, ctx, containerModel); } }
@Override protected void serializeInternal(T collection, JsonGenerator generator, SerializationContext ctx) { for (Object item : collection) { serializeItem(item, generator, ctx, containerModel); } }
@SuppressWarnings("unchecked") private <T> void serialCaptor(JsonbSerializer<?> serializer, T object, JsonGenerator generator, SerializationContext context) { ((JsonbSerializer<T>) serializer).serialize(object, generator, context); }
@Override protected void serializeInternal(double[] arr, JsonGenerator generator, SerializationContext ctx) { for (double obj : arr) { generator.write(obj); } }
@Override protected void serializeInternal(JsonArray obj, JsonGenerator generator, SerializationContext ctx) { for (JsonValue value : obj) { generator.write(value); } }
@Override protected void serializeInternal(byte[] obj, JsonGenerator generator, SerializationContext ctx) { for (byte b : obj) { generator.write(b); } }
@Override protected void serializeInternal(short[] arr, JsonGenerator generator, SerializationContext ctx) { for (short obj : arr) { generator.write(obj); } }
@Override protected void serializeInternal(int[] arr, JsonGenerator generator, SerializationContext ctx) { for (int obj : arr) { generator.write(obj); } }
@Override protected void serializeInternal(JsonObject obj, JsonGenerator generator, SerializationContext ctx) { for (Map.Entry<String, JsonValue> entry : obj.entrySet()) { generator.write(entry.getKey(), entry.getValue()); } }
@Override public void serialize(AnnotatedWithSerializerType obj, JsonGenerator generator, SerializationContext ctx) { generator.writeStartObject(); generator.write("valueField", "replaced value"); generator.writeEnd(); }
@Override public void serialize(AnnotatedWithSerializerType obj, JsonGenerator generator, SerializationContext ctx) { generator.writeStartObject(); generator.write("valueField", "overridden value"); generator.writeEnd(); }
@Override public void serialize(String obj, JsonGenerator generator, SerializationContext ctx) { generator.write(obj.substring(0, 1)); }
@Override public void serialize(String obj, JsonGenerator generator, SerializationContext ctx) { generator.write(" "+obj); }
@Override public void serialize(Box box, JsonGenerator jsonGenerator, SerializationContext serializationContext) { jsonGenerator.writeStartObject(); serializationContext.serialize("boxFieldName", box, jsonGenerator); jsonGenerator.writeEnd(); }
@Override public void serialize(final Whole2 obj, final JsonGenerator generator, final SerializationContext ctx) { generator.write("text", obj.name); }
@Override public void serialize(final Foo obj, final JsonGenerator generator, final SerializationContext ctx) { generator.write("full", true); generator.write("name", obj.name); }
@Override public void serialize(CompileTimeSampler obj, JsonGenerator generator, SerializationContext ctx) { }
/** * Serializes an object to JSON. * * @param obj Object to serialize. * @param generator JSON generator to use. * @param ctx JSON-B mapper context. */ @Override public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { Marshaller marshaller = (Marshaller) ctx; serialize(obj, generator, marshaller); }
protected abstract void serializeInternal(T obj, JsonGenerator generator, SerializationContext ctx);