Java 类javax.json.bind.serializer.SerializationContext 实例源码

项目:Java-EE-8-Sampler    文件:BookSerializer.java   
@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);
    }
}
项目:Java-EE-8-Sampler    文件:MagazineSerializer.java   
@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);
    }

}
项目:Java-EE-8-Sampler    文件:BookSerializer.java   
@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();
}
项目:scalable-coffee-shop    文件:EventJsonbSerializer.java   
@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();
}
项目:scalable-coffee-shop    文件:EventJsonbSerializer.java   
@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();
}
项目:scalable-coffee-shop    文件:EventJsonbSerializer.java   
@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();
}
项目:yasson    文件:AbstractContainerSerializer.java   
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);
}
项目:yasson    文件:ObjectSerializer.java   
@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);
    }
}
项目:yasson    文件:ObjectSerializer.java   
@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);
    }
}
项目:yasson    文件:MapSerializer.java   
@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);
    }
}
项目:yasson    文件:FloatArraySerializer.java   
@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)));
    }
}
项目:yasson    文件:AdaptedObjectSerializer.java   
@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());
    }
}
项目:yasson    文件:OptionalObjectSerializer.java   
@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);
}
项目:yasson    文件:UserSerializerSerializer.java   
@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());
    }
}
项目:yasson    文件:SimpleContainerArraySerializer.java   
@Override
public void serialize(SimpleContainer[] containers, JsonGenerator jsonGenerator, SerializationContext serializationContext) {
    jsonGenerator.writeStartArray();
    for (SimpleContainer container : containers) {
        serializationContext.serialize(container, jsonGenerator);
    }
    jsonGenerator.writeEnd();
}
项目:yasson    文件:CrateSerializer.java   
@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();
}
项目:yasson    文件:CrateSerializerWithConversion.java   
@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();
}
项目:yasson    文件:AbstractContainerSerializer.java   
@Override
public final void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
    writeStart(generator);
    serializeInternal(obj, generator, ctx);
    writeEnd(generator);
}
项目:yasson    文件:AbstractContainerSerializer.java   
@SuppressWarnings("unchecked")
protected <X> void serializerCaptor(JsonbSerializer<?> serializer, X object, JsonGenerator generator, SerializationContext ctx) {
    ((JsonbSerializer<X>) serializer).serialize(object, generator, ctx);
}
项目:yasson    文件:AbstractDateTimeSerializer.java   
@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));
}
项目:yasson    文件:LongArraySerializer.java   
@Override
protected void serializeInternal(long[] arr, JsonGenerator generator, SerializationContext ctx) {
    for (long obj : arr) {
        generator.write(obj);
    }
}
项目:yasson    文件:ObjectArraySerializer.java   
@Override
protected void serializeInternal(T[] arr, JsonGenerator generator, SerializationContext ctx) {
    for (T obj : arr) {
        serializeItem(obj, generator, ctx, containerModel);
    }
}
项目:yasson    文件:CollectionSerializer.java   
@Override
protected void serializeInternal(T collection, JsonGenerator generator, SerializationContext ctx) {
    for (Object item : collection) {
        serializeItem(item, generator, ctx, containerModel);
    }
}
项目:yasson    文件:OptionalObjectSerializer.java   
@SuppressWarnings("unchecked")
private <T> void serialCaptor(JsonbSerializer<?> serializer, T object, JsonGenerator generator, SerializationContext context) {
    ((JsonbSerializer<T>) serializer).serialize(object, generator, context);
}
项目:yasson    文件:DoubleArraySerializer.java   
@Override
protected void serializeInternal(double[] arr, JsonGenerator generator, SerializationContext ctx) {
    for (double obj : arr) {
        generator.write(obj);
    }
}
项目:yasson    文件:JsonArraySerializer.java   
@Override
protected void serializeInternal(JsonArray obj, JsonGenerator generator, SerializationContext ctx) {
    for (JsonValue value : obj) {
        generator.write(value);
    }
}
项目:yasson    文件:ByteArraySerializer.java   
@Override
protected void serializeInternal(byte[] obj, JsonGenerator generator, SerializationContext ctx) {
    for (byte b : obj) {
        generator.write(b);
    }
}
项目:yasson    文件:ShortArraySerializer.java   
@Override
protected void serializeInternal(short[] arr, JsonGenerator generator, SerializationContext ctx) {
    for (short obj : arr) {
        generator.write(obj);
    }
}
项目:yasson    文件:IntArraySerializer.java   
@Override
protected void serializeInternal(int[] arr, JsonGenerator generator, SerializationContext ctx) {
    for (int obj : arr) {
        generator.write(obj);
    }
}
项目:yasson    文件:JsonObjectSerializer.java   
@Override
protected void serializeInternal(JsonObject obj, JsonGenerator generator, SerializationContext ctx) {
    for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {
        generator.write(entry.getKey(), entry.getValue());
    }
}
项目:yasson    文件:AnnotatedWithSerializerTypeSerializer.java   
@Override
public void serialize(AnnotatedWithSerializerType obj, JsonGenerator generator, SerializationContext ctx) {
    generator.writeStartObject();
    generator.write("valueField", "replaced value");
    generator.writeEnd();
}
项目:yasson    文件:AnnotatedWithSerializerTypeSerializerOverride.java   
@Override
public void serialize(AnnotatedWithSerializerType obj, JsonGenerator generator, SerializationContext ctx) {
    generator.writeStartObject();
    generator.write("valueField", "overridden value");
    generator.writeEnd();
}
项目:yasson    文件:Author.java   
@Override
public void serialize(String obj, JsonGenerator generator, SerializationContext ctx) {
    generator.write(obj.substring(0, 1));
}
项目:yasson    文件:StringPaddingSerializer.java   
@Override
public void serialize(String obj, JsonGenerator generator, SerializationContext ctx) {
    generator.write("   "+obj);
}
项目:yasson    文件:RecursiveSerializer.java   
@Override
public void serialize(Box box, JsonGenerator jsonGenerator, SerializationContext serializationContext) {
    jsonGenerator.writeStartObject();
    serializationContext.serialize("boxFieldName", box, jsonGenerator);
    jsonGenerator.writeEnd();
}
项目:johnzon    文件:ClassConverterTest.java   
@Override
public void serialize(final Whole2 obj, final JsonGenerator generator, final SerializationContext ctx) {
    generator.write("text", obj.name);
}
项目:johnzon    文件:SerializerTest.java   
@Override
public void serialize(final Foo obj, final JsonGenerator generator, final SerializationContext ctx) {
    generator.write("full", true);
    generator.write("name", obj.name);
}
项目:JSON-framework-comparison    文件:CustomSerializer.java   
@Override
public void serialize(CompileTimeSampler obj, JsonGenerator generator, SerializationContext ctx) {

}
项目:yasson    文件:AbstractValueTypeSerializer.java   
/**
 * 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);
}
项目:yasson    文件:AbstractContainerSerializer.java   
protected abstract void serializeInternal(T obj, JsonGenerator generator, SerializationContext ctx);