Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for canonical JSON output in Jackson 2.16 #3967

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.fasterxml.jackson.databind.ser;

import java.io.IOException;
import java.math.BigDecimal;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class CanonicalBigDecimalSerializer extends StdSerializer<BigDecimal>
implements ValueToString<BigDecimal> {
private static final long serialVersionUID = 1L;

public static final CanonicalBigDecimalSerializer INSTANCE = new CanonicalBigDecimalSerializer();

public static final CanonicalNumberSerializerProvider PROVIDER = new CanonicalNumberSerializerProvider() {
@Override
public StdSerializer<BigDecimal> getNumberSerializer() {
return INSTANCE;
}

@Override
public ValueToString<BigDecimal> getValueToString() {
return INSTANCE;
}
};

protected CanonicalBigDecimalSerializer() {
super(BigDecimal.class);
}

@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
CanonicalNumberGenerator.verifyBigDecimalRange(value, provider);

String output = convert(value);
gen.writeNumber(output);
}

@Override
public String convert(BigDecimal value) {
// TODO Convert to exponential form if necessary
BigDecimal stripped = value.stripTrailingZeros();
int scale = stripped.scale();
String text = stripped.toPlainString();
if (scale == 0) {
return text;
}

int pos = text.indexOf('.');
int exp;
if (pos >= 0) {
exp = pos - 1;

if (exp == 0) {
return text;
}

text = text.substring(0, pos) + text.substring(pos + 1);
} else {
exp = -scale;
int end = text.length();
while (end > 0 && text.charAt(end - 1) == '0') {
end --;
}
text = text.substring(0, end);
}

if (text.length() == 1) {
return text + 'E' + exp;
}

return text.substring(0, 1) + '.' + text.substring(1) + 'E' + exp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.fasterxml.jackson.databind.ser;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;

import org.junit.jupiter.api.Test;

public class CanonicalBigDecimalSerializerTest {

@Test
void testCanonicalDecimalHandling_1() throws Exception {
assertSerialized("1", new BigDecimal("1"));
}

@Test
void testCanonicalDecimalHandling_1_000() throws Exception {
assertSerialized("1", new BigDecimal("1.000"));
}

@Test
void testCanonicalDecimalHandling_10_1000() throws Exception {
assertSerialized("1.01E1", new BigDecimal("10.1000"));
}

@Test
void testCanonicalDecimalHandling_1000() throws Exception {
assertSerialized("1E3", new BigDecimal("1000"));
}

@Test
void testCanonicalDecimalHandling_0_00000000010() throws Exception {
assertSerialized("0.0000000001", new BigDecimal("0.00000000010"));
}

@Test
void testCanonicalDecimalHandling_1000_00010() throws Exception {
assertSerialized("1.0000001E3", new BigDecimal("1000.00010"));
}

@Test
void testCanonicalHugeDecimalHandling() throws Exception {
BigDecimal actual = new BigDecimal("123456789123456789123456789123456789.123456789123456789123456789123456789123456789000");
assertSerialized("1.23456789123456789123456789123456789123456789123456789123456789123456789123456789E35", actual);
}

private void assertSerialized(String expected, BigDecimal actual) {
CanonicalBigDecimalSerializer serializer = new CanonicalBigDecimalSerializer();
assertEquals(expected, serializer.convert(actual));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fasterxml.jackson.databind.ser;

import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.JsonFactory;

/**
* TODO Fix double numbers. This feels like a very heavy solution plus I can't
* use the JsonFactory.builder().
*/
public class CanonicalJsonFactory extends JsonFactory {
private static final long serialVersionUID = 1L;

private ValueToString<BigDecimal> _serializer;

public CanonicalJsonFactory(ValueToString<BigDecimal> serializer) {
this._serializer = serializer;
}

@Override
protected JsonGenerator _createGenerator(Writer out, IOContext ioCtxt)
digulla marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
JsonGenerator delegate = super._createGenerator(out, ioCtxt);
return new CanonicalNumberGenerator(delegate, _serializer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.fasterxml.jackson.databind.ser;

import com.fasterxml.jackson.core.StreamWriteFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class CanonicalJsonMapper { // TODO It would be great if we could extend JsonMapper but the return type of builder() is incompatible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not going to be possible since with 3.0 especially there are be per-format sub-classes of ObjectMapper (2.x actually has these too, but with 3.0 it is more fundamentally so, to match particular TokenStreamFactory with matching ObjectMapper subtype).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... I feel that the implementation of the builders as is leads to a lot of code duplication. Any ideas how to improve this?


public static class Builder { // TODO Can't extend MapperBuilder<JsonMapper, Builder> because that needs JsonFactory as ctor arg and we only have this later
private CanonicalNumberSerializerProvider _numberSerializerProvider = CanonicalBigDecimalSerializer.PROVIDER;
private boolean _enablePrettyPrinting = false;

private Builder() {
// Don't allow to create except via builder method
}

public Builder prettyPrint() {
_enablePrettyPrinting = true;
_numberSerializerProvider = PrettyBigDecimalSerializer.PROVIDER;
return this;
}

public JsonMapper build() {
CanonicalJsonFactory jsonFactory = new CanonicalJsonFactory(_numberSerializerProvider.getValueToString());
CanonicalJsonModule module = new CanonicalJsonModule(_numberSerializerProvider.getNumberSerializer());

JsonMapper.Builder builder = JsonMapper.builder(jsonFactory)
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS) //
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) //
.addModule(module);

if (_enablePrettyPrinting) {
builder = builder //
.enable(SerializationFeature.INDENT_OUTPUT) //
.enable(StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN) //
.defaultPrettyPrinter(CanonicalPrettyPrinter.INSTANCE) //
;
}

return builder.build();
}
}

public static CanonicalJsonMapper.Builder builder() {
return new Builder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fasterxml.jackson.databind.ser;

import java.math.BigDecimal;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class CanonicalJsonModule extends SimpleModule {
private static final long serialVersionUID = 1L;

public CanonicalJsonModule() {
this(CanonicalBigDecimalSerializer.INSTANCE);
}

public CanonicalJsonModule(StdSerializer<BigDecimal> numberSerializer) {
addSerializer(BigDecimal.class, numberSerializer);
}
}
Loading