Re: [jackson-user] The correct way to serialize / deserialize a Map to / from XML

2018-09-27 Thread Tatu Saloranta
On Thu, Sep 27, 2018 at 10:29 PM Tristan Lins 
wrote:

> I need to serialize / deserialize a Map to / from XML with
> dataformat-xml / XmlMapper.
>
> It works fine with JSON when using default typing.
> But when deserialize the XML, the typing of primitive types (especially
> numeric and boolean) get lost.
>
> Every number and boolean is treated as string, when deserializing.
>
> java.lang.AssertionError: expected: java.lang.Integer<123> but was: java.
> lang.String<123>
>
> At the moment I manage with my own module and serializers for the
> primitive types.
> This works fine only with JsonTypeInfo.As.WRAPPER_OBJECT, but with
> JsonTypeInfo.As.WRAPPER_ARRAY, deserialization fails.
> Attached is a unit test and the module.
>
> But I wonder if there is not a better way?
>

Maps and XML are problematic as XML is inherently text based and has no
mechanism (in base XML specification, not including optional XML Schema
languages and definitions) for differentiating between numbers, strings and
booleans. Or between Arrays and Objects for that matter.

Couple of approaches you can use to start binding string values are:

1. Use of POJOs: Jackson can coerce String values into numbers (etc)
2. Two-pass processing: first into intermediate structures, then use your
own detection to derive numbers
3. Custom deserializer for `Map` type.

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[jackson-user] The correct way to serialize / deserialize a Map to / from XML

2018-09-27 Thread Tristan Lins
I need to serialize / deserialize a Map to / from XML with 
dataformat-xml / XmlMapper.

It works fine with JSON when using default typing.
But when deserialize the XML, the typing of primitive types (especially 
numeric and boolean) get lost.

Every number and boolean is treated as string, when deserializing.

java.lang.AssertionError: expected: java.lang.Integer<123> but was: java.
lang.String<123>

At the moment I manage with my own module and serializers for the primitive 
types.
This works fine only with JsonTypeInfo.As.WRAPPER_OBJECT, but with 
JsonTypeInfo.As.WRAPPER_ARRAY, deserialization fails.
Attached is a unit test and the module.

But I wonder if there is not a better way?

Best regards
Tristan

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;

import java.io.IOException;

public class CompletelyTypedJacksonModule extends SimpleModule {
public CompletelyTypedJacksonModule() {
addSerializer(String.class, new TypedStringSerializer());
addSerializer(Boolean.class, new TypedBooleanSerializer());
addSerializer(Short.class, new TypedShortSerializer());
addSerializer(Integer.class, new TypedIntegerSerializer());
addSerializer(Float.class, new TypedFloatSerializer());
addSerializer(Double.class, new TypedDoubleSerializer());
}

public static class TypedStringSerializer extends StdScalarSerializer {
public TypedStringSerializer() {
super(String.class, false);
}

@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeString(value);
}
}
}

public static class TypedBooleanSerializer extends StdScalarSerializer {
public TypedBooleanSerializer() {
super(Boolean.class, false);
}

@Override
public void serialize(Boolean value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeBoolean(value);
}
}
}

public static class TypedByteSerializer extends StdScalarSerializer {
public TypedByteSerializer() {
super(Byte.class, false);
}

@Override
public void serialize(Byte value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeNumber(value);
}
}
}

public static class TypedShortSerializer extends StdScalarSerializer {
public TypedShortSerializer() {
super(Short.class, false);
}

@Override
public void serialize(Short value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeNumber(value);
}
}
}

public static class TypedIntegerSerializer extends StdScalarSerializer {
public TypedIntegerSerializer() {
super(Integer.class, false);
}

@Override
public void serialize(Integer value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeNumber(value);
}
}
}

public static class TypedFloatSerializer extends StdScalarSerializer {
public TypedFloatSerializer() {
super(Float.class, false);
}

@Override
public void serialize(Float value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeNumber(value);
}
}
}

public static class TypedDoubleSerializer extends StdScalarSerializer {
public TypedDoubleSerializer() {
super(Double.class, false);
}

@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (null == value) {
gen.writeNull();
} else {
gen.writeNumb