List of usage examples for com.fasterxml.jackson.databind JsonSerializer serialize
public abstract void serialize(T paramT, JsonGenerator paramJsonGenerator, SerializerProvider paramSerializerProvider);
From source file:io.airlift.event.client.JsonEventWriter.java
public <T> void writeEvents(EventClient.EventGenerator<T> events, OutputStream out) throws IOException { Preconditions.checkNotNull(events, "events is null"); Preconditions.checkNotNull(out, "out is null"); final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); jsonGenerator.writeStartArray();/*from w ww . j a va 2s.c o m*/ events.generate(new EventClient.EventPoster<T>() { @Override public void post(T event) throws IOException { JsonSerializer<T> serializer = getSerializer(event); if (serializer == null) { throw new InvalidEventException("Event class [%s] has not been registered as an event", event.getClass().getName()); } serializer.serialize(event, jsonGenerator, null); } }); jsonGenerator.writeEndArray(); jsonGenerator.flush(); }
From source file:com.proofpoint.event.client.JsonEventWriter.java
public <T> void writeEvents(EventGenerator<T> events, @Nullable final String token, OutputStream out) throws IOException { checkNotNull(events, "events is null"); checkNotNull(out, "out is null"); final JsonGenerator jsonGenerator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); jsonGenerator.writeStartArray();//from w w w .j a va 2s .c o m events.generate(new EventClient.EventPoster<T>() { @Override public void post(T event) throws IOException { JsonSerializer<T> serializer = getSerializer(event, token); if (serializer == null) { throw new InvalidEventException("Event class [%s] has not been registered as an event", event.getClass().getName()); } serializer.serialize(event, jsonGenerator, null); } }); jsonGenerator.writeEndArray(); jsonGenerator.flush(); }
From source file:com.proofpoint.event.client.JsonEventWriter.java
public <T> Writer createEventWriter(final Iterator<T> eventIterator, final String token, final OutputStream out) throws IOException { checkNotNull(eventIterator, "eventIterator is null"); checkNotNull(out, "out is null"); final JsonGenerator jsonGenerator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); jsonGenerator.writeStartArray();/*from ww w.j a v a 2 s.c o m*/ return new Writer() { @Override public void write() throws Exception { if (eventIterator.hasNext()) { T event = eventIterator.next(); JsonSerializer<T> serializer = getSerializer(event, token); if (serializer == null) { throw new InvalidEventException("Event class [%s] has not been registered as an event", event.getClass().getName()); } serializer.serialize(event, jsonGenerator, null); } else { jsonGenerator.writeEndArray(); jsonGenerator.flush(); out.close(); } } }; }
From source file:javaslang.jackson.datatype.serialize.EitherSerializer.java
private void write(Object val, int containedTypeIndex, JsonGenerator gen, SerializerProvider provider) throws IOException { if (val != null) { if (type.containedTypeCount() > containedTypeIndex) { JsonSerializer<Object> ser; JavaType containedType = type.containedType(containedTypeIndex); if (containedType.getRawClass() != Object.class) { ser = provider.findTypedValueSerializer(type.containedType(containedTypeIndex), true, null); } else { ser = provider.findTypedValueSerializer(val.getClass(), true, null); }//w w w . jav a2 s . c o m ser.serialize(val, gen, provider); } else { gen.writeObject(val); } } else { gen.writeNull(); } }
From source file:javaslang.jackson.datatype.serialize.ValueSerializer.java
@Override public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException { Object obj = toJavaObj(value); if (obj == null) { provider.getDefaultNullValueSerializer().serialize(null, gen, provider); } else {/*from ww w. ja v a2s. c om*/ JsonSerializer<Object> ser; try { JavaType emulated = emulatedJavaType(type); if (emulated.getRawClass() != Object.class) { ser = provider.findTypedValueSerializer(emulated, true, null); } else { ser = provider.findTypedValueSerializer(obj.getClass(), true, null); } } catch (Exception ignore) { ser = provider.findTypedValueSerializer(obj.getClass(), true, null); } ser.serialize(obj, gen, provider); } }
From source file:de.ks.flatadocdb.defaults.json.RelationCollectionPropertyWriter.java
@Override public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { Object value = (_accessorMethod == null) ? _field.get(bean) : _accessorMethod.invoke(bean); if (value instanceof Collection) { Collection<String> ids; if (value instanceof List) { ids = new ArrayList<>(); } else {/*from w w w . ja va2 s . c o m*/ ids = new HashSet<>(); } for (Object object : ((Collection) value)) { if (object != null) { EntityDescriptor entityDescriptor = metaModel.getEntityDescriptor(object.getClass()); String id = entityDescriptor.getId(object); log.trace("Replaced object {} of collection by relation id {}.", object, id); ids.add(id); } } value = ids; } else if (value != null) { EntityDescriptor entityDescriptor = metaModel.getEntityDescriptor(value.getClass()); String id = entityDescriptor.getId(value); value = id; log.trace("Replaced single object {} by relation id {}.", value, id); } // Null handling is bit different, check that first if (value == null) { if (_nullSerializer != null) { gen.writeFieldName(_name); _nullSerializer.serialize(null, gen, prov); } return; } // then find serializer to use JsonSerializer<Object> ser = _serializer; if (ser == null) { Class<?> cls = value.getClass(); PropertySerializerMap m = _dynamicSerializers; ser = m.serializerFor(cls); if (ser == null) { ser = _findAndAddDynamic(m, cls, prov); } } // and then see if we must suppress certain values (default, empty) if (_suppressableValue != null) { if (MARKER_FOR_EMPTY == _suppressableValue) { if (ser.isEmpty(prov, value)) { return; } } else if (_suppressableValue.equals(value)) { return; } } // For non-nulls: simple check for direct cycles if (value == bean) { // three choices: exception; handled by call; or pass-through if (_handleSelfReference(bean, gen, prov, ser)) { return; } } gen.writeFieldName(_name); if (_typeSerializer == null) { ser.serialize(value, gen, prov); } else { ser.serializeWithType(value, gen, prov, _typeSerializer); } }
From source file:org.mongojack.internal.util.SerializationUtils.java
private static Object serializeQueryField(Object value, JsonSerializer serializer, SerializerProvider serializerProvider, String op) { if (serializer == null) { if (value == null || BASIC_TYPES.contains(value.getClass())) { // Return as is return value; } else if (value instanceof Collection) { Collection<?> coll = (Collection<?>) value; List<Object> copy = null; int position = 0; for (Object item : coll) { Object returned = serializeQueryField(item, null, serializerProvider, op); if (returned != item) { if (copy == null) { copy = new ArrayList<Object>(coll); }//from w w w . j a v a2 s . c o m copy.set(position, returned); } position++; } if (copy != null) { return copy; } else { return coll; } } else if (value.getClass().isArray()) { if (BASIC_TYPES.contains(value.getClass().getComponentType())) { return value; } Object[] array = (Object[]) value; Object[] copy = null; for (int i = 0; i < array.length; i++) { Object returned = serializeQueryField(array[i], null, serializerProvider, op); if (returned != array[i]) { if (copy == null) { copy = new Object[array.length]; System.arraycopy(array, 0, copy, 0, array.length); } copy[i] = returned; } } if (copy != null) { return copy; } else { return array; } } else { // We don't know what it is, just find a serializer for it serializer = JacksonAccessor.findValueSerializer(serializerProvider, value.getClass()); } } BsonObjectGenerator objectGenerator = new BsonObjectGenerator(); try { serializer.serialize(value, objectGenerator, serializerProvider); } catch (IOException e) { throw new MongoJsonMappingException("Error serializing value " + value + " in DBQuery operation " + op, e); } return objectGenerator.getValue(); }