Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 29, 2017
1 parent f6f55a8 commit d2d2e77
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,41 +190,21 @@ public void writeTypePrefixForArray(Object value, JsonGenerator g, Class<?> type
/**********************************************************
*/

/**
* Method called to write initial part of type information for given
* value, when it will be output as scalar JSON value (not as JSON
* Object or Array),
* using specified custom type id instead of calling {@link TypeIdResolver}.
* This means that the context after call can not be that of JSON Object;
* it may be Array or root context.
*
* @param value Value that will be serialized, for which type information is
* to be written
* @param g Generator to use for writing type information
* @param typeId Exact type id to use
*/
public abstract void writeCustomTypePrefixForScalar(Object value, JsonGenerator g, String typeId) throws IOException;
// @Deprecated // since 2.9
public void writeCustomTypePrefixForScalar(Object value, JsonGenerator g, String typeId) throws IOException { }

/**
* Method called to write initial part of type information for given
* value, when it will be output as JSON Object value (not as JSON
* Array or scalar),
* using specified custom type id instead of calling {@link TypeIdResolver}.
* This means that context after call must be JSON Object, meaning that
* caller can then proceed to output field entries.
*
* @param value Value that will be serialized, for which type information is
* to be written
* @param g Generator to use for writing type information
* @param typeId Exact type id to use
*/
public abstract void writeCustomTypePrefixForObject(Object value, JsonGenerator g, String typeId) throws IOException;

public abstract void writeCustomTypePrefixForArray(Object value, JsonGenerator g, String typeId) throws IOException;
// @Deprecated // since 2.9
public void writeCustomTypePrefixForObject(Object value, JsonGenerator g, String typeId) throws IOException { }

// @Deprecated // since 2.9
public void writeCustomTypePrefixForArray(Object value, JsonGenerator g, String typeId) throws IOException { }

public abstract void writeCustomTypeSuffixForScalar(Object value, JsonGenerator g, String typeId) throws IOException;
// @Deprecated // since 2.9
public void writeCustomTypeSuffixForScalar(Object value, JsonGenerator g, String typeId) throws IOException { }

public abstract void writeCustomTypeSuffixForObject(Object value, JsonGenerator g, String typeId) throws IOException;
// @Deprecated // since 2.9
public void writeCustomTypeSuffixForObject(Object value, JsonGenerator g, String typeId) throws IOException { }

public abstract void writeCustomTypeSuffixForArray(Object value, JsonGenerator g, String typeId) throws IOException;
// @Deprecated // since 2.9
public void writeCustomTypeSuffixForArray(Object value, JsonGenerator g, String typeId) throws IOException { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected TypeSerializerBase(TypeIdResolver idRes, BeanProperty property)
@Override
public TypeIdResolver getTypeIdResolver() { return _idResolver; }

@Override
public void writeTypePrefix(JsonGenerator g,
WritableTypeId idMetadata) throws IOException
{
Expand All @@ -55,9 +56,7 @@ public void writeTypePrefix(JsonGenerator g,
}
}

/**
* @since 2.9
*/
@Override
public void writeTypeSuffix(JsonGenerator g,
WritableTypeId idMetadata) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ public void serializeWithType(Object bean, JsonGenerator gen,
_serializeWithObjectId(bean, gen, provider, typeSer);
return;
}
WritableTypeId typeIdDef;
if (_typeId == null) {
typeIdDef = new WritableTypeId(bean, JsonToken.START_ARRAY);
} else {
typeIdDef = new WritableTypeId(bean, JsonToken.START_ARRAY,
_customTypeId(bean));
}
WritableTypeId typeIdDef = _typeIdDef(bean, JsonToken.START_ARRAY);
/*
if (typeStr == null) {
typeSer.writeTypePrefixForArray(bean, gen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public void serializeWithType(Object bean, JsonGenerator gen,
return;
}

WritableTypeId typeIdDef = _typeIdDef(bean);
WritableTypeId typeIdDef = _typeIdDef(bean, JsonToken.START_OBJECT);
gen.setCurrentValue(bean); // [databind#631]
typeSer.writeTypePrefix(gen, typeIdDef);

Expand Down Expand Up @@ -658,7 +658,7 @@ protected void _serializeObjectId(Object bean, JsonGenerator gen,SerializerProv
TypeSerializer typeSer, WritableObjectId objectId) throws IOException
{
final ObjectIdWriter w = _objectIdWriter;
WritableTypeId typeIdDef = _typeIdDef(bean);
WritableTypeId typeIdDef = _typeIdDef(bean, JsonToken.START_OBJECT);

typeSer.writeTypePrefix(gen, typeIdDef);
objectId.writeAsField(gen, provider, w);
Expand All @@ -673,16 +673,16 @@ protected void _serializeObjectId(Object bean, JsonGenerator gen,SerializerProv
/**
* @since 2.9
*/
protected final WritableTypeId _typeIdDef(Object bean) {
protected final WritableTypeId _typeIdDef(Object bean, JsonToken valueShape) {
if (_typeId == null) {
return new WritableTypeId(bean, JsonToken.START_OBJECT);
return new WritableTypeId(bean, valueShape);
}
Object typeId = _typeId.getValue(bean);
if (typeId == null) {
// 28-Jun-2017, tatu: Is this really needed? Unchanged from 2.8 but...
typeId = "";
}
return new WritableTypeId(bean, JsonToken.START_OBJECT, typeId);
return new WritableTypeId(bean, valueShape, typeId);
}

@Deprecated // since 2.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand Down Expand Up @@ -85,12 +86,14 @@ public void serialize(InetAddress value, JsonGenerator g, SerializerProvider pro
}

@Override
public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer) throws IOException, JsonGenerationException
public void serializeWithType(InetAddress value, JsonGenerator g, SerializerProvider provider,
TypeSerializer typeSer) throws IOException
{
// Better ensure we don't use specific sub-classes...
typeSer.writeTypePrefixForScalar(value, jgen, InetAddress.class);
serialize(value, jgen, provider);
typeSer.writeTypeSuffixForScalar(value, jgen);
WritableTypeId typeIdDef = new WritableTypeId(value, InetAddress.class,
JsonToken.VALUE_STRING);
typeSer.writeTypePrefix(g, typeIdDef);
serialize(value, g, provider);
typeSer.writeTypeSuffix(g, typeIdDef);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.fasterxml.jackson.databind.ser.std;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

/**
* Simple serializer for {@link InetSocketAddress}.
*/
Expand Down Expand Up @@ -40,11 +41,14 @@ public void serialize(InetSocketAddress value, JsonGenerator jgen, SerializerPro
}

@Override
public void serializeWithType(InetSocketAddress value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException, JsonGenerationException
public void serializeWithType(InetSocketAddress value, JsonGenerator g,
SerializerProvider provider, TypeSerializer typeSer) throws IOException
{
// Better ensure we don't use specific sub-classes...
typeSer.writeTypePrefixForScalar(value, jgen, InetSocketAddress.class);
serialize(value, jgen, provider);
typeSer.writeTypeSuffixForScalar(value, jgen);
WritableTypeId typeIdDef = new WritableTypeId(value,
InetSocketAddress.class, JsonToken.VALUE_STRING);
typeSer.writeTypePrefix(g, typeIdDef);
serialize(value, g, provider);
typeSer.writeTypeSuffix(g, typeIdDef);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.TimeZone;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

Expand All @@ -13,15 +14,19 @@ public class TimeZoneSerializer extends StdScalarSerializer<TimeZone>
public TimeZoneSerializer() { super(TimeZone.class); }

@Override
public void serialize(TimeZone value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(value.getID());
public void serialize(TimeZone value, JsonGenerator g, SerializerProvider provider) throws IOException {
g.writeString(value.getID());
}

@Override
public void serializeWithType(TimeZone value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException {
public void serializeWithType(TimeZone value, JsonGenerator g,
SerializerProvider provider, TypeSerializer typeSer) throws IOException
{
// Better ensure we don't use specific sub-classes:
typeSer.writeTypePrefixForScalar(value, jgen, TimeZone.class);
serialize(value, jgen, provider);
typeSer.writeTypeSuffixForScalar(value, jgen);
WritableTypeId typeIdDef = new WritableTypeId(value,
TimeZone.class, JsonToken.VALUE_STRING);
typeSer.writeTypePrefix(g, typeIdDef);
serialize(value, g, provider);
typeSer.writeTypeSuffix(g, typeIdDef);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testDateTypes() throws Exception
inputList.add(Locale.CHINESE);
input.values = inputList;
String json = m.writeValueAsString(input);

ObjectListBean output = m.readValue(json, ObjectListBean.class);
List<Object> outputList = output.values;
assertEquals(2, outputList.size());
Expand Down

0 comments on commit d2d2e77

Please sign in to comment.