Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 27, 2018
1 parent 396c4da commit 5f45318
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 51 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/json/JsonMapper.java
Expand Up @@ -2,11 +2,15 @@

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.json.JsonWriteFeature;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
import com.fasterxml.jackson.databind.cfg.PackageVersion;

/**
* JSON-format specific {@link ObjectMapper} implementation.
*
* @since 2.10
*/
Expand All @@ -25,6 +29,54 @@ public static class Builder extends MapperBuilder<JsonMapper, Builder>
public Builder(JsonMapper m) {
super(m);
}

public Builder enable(JsonReadFeature... features) {
for (JsonReadFeature f : features) {
_mapper.enable(f.mappedFeature());
}
return this;
}

public Builder disable(JsonReadFeature... features) {
for (JsonReadFeature f : features) {
_mapper.disable(f.mappedFeature());
}
return this;
}

public Builder configure(JsonReadFeature f, boolean state)
{
if (state) {
_mapper.enable(f.mappedFeature());
} else {
_mapper.disable(f.mappedFeature());
}
return this;
}

public Builder enable(JsonWriteFeature... features) {
for (JsonWriteFeature f : features) {
_mapper.enable(f.mappedFeature());
}
return this;
}

public Builder disable(JsonWriteFeature... features) {
for (JsonWriteFeature f : features) {
_mapper.disable(f.mappedFeature());
}
return this;
}

public Builder configure(JsonWriteFeature f, boolean state)
{
if (state) {
_mapper.enable(f.mappedFeature());
} else {
_mapper.disable(f.mappedFeature());
}
return this;
}
}

/*
Expand Down Expand Up @@ -81,4 +133,21 @@ public Version version() {
public JsonFactory getFactory() {
return _jsonFactory;
}

/*
/**********************************************************
/* JSON-specific accessors, mutators
/**********************************************************
*/

// // // 25-Oct-2018, tatu: Since for 2.x these will simply map to legacy settings,
// // // we will fake them

public boolean isEnabled(JsonReadFeature f) {
return isEnabled(f.mappedFeature());
}

public boolean isEnabled(JsonWriteFeature f) {
return isEnabled(f.mappedFeature());
}
}
Expand Up @@ -6,7 +6,9 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class MapperViaParserTest extends BaseMapTest
{
Expand Down Expand Up @@ -94,60 +96,58 @@ public void testPojoReading() throws IOException
/**
* Test similar to above, but instead reads a sequence of values
*/
public void testIncrementalPojoReading()
throws IOException
public void testIncrementalPojoReading() throws IOException
{
JsonFactory jf = new MappingJsonFactory();
final String JSON = "[ 1, true, null, \"abc\" ]";
JsonParser jp = jf.createParser(new StringReader(JSON));
JsonParser p = jf.createParser(new StringReader(JSON));

// let's advance past array start to prevent full binding
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.START_ARRAY, p.nextToken());

assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(Integer.valueOf(1), jp.readValueAs(Integer.class));
assertEquals(Boolean.TRUE, jp.readValueAs(Boolean.class));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(Integer.valueOf(1), p.readValueAs(Integer.class));
assertEquals(Boolean.TRUE, p.readValueAs(Boolean.class));
/* note: null can be returned both when there is no more
* data in current scope, AND when Json null literal is
* bound!
*/
assertNull(jp.readValueAs(Object.class));
assertNull(p.readValueAs(Object.class));
// but we can verify that it was Json null by:
assertEquals(JsonToken.VALUE_NULL, jp.getLastClearedToken());
assertEquals(JsonToken.VALUE_NULL, p.getLastClearedToken());

assertEquals("abc", jp.readValueAs(String.class));
assertEquals("abc", p.readValueAs(String.class));

// this null is for actually hitting the END_ARRAY
assertNull(jp.readValueAs(Object.class));
assertEquals(JsonToken.END_ARRAY, jp.getLastClearedToken());
assertNull(p.readValueAs(Object.class));
assertEquals(JsonToken.END_ARRAY, p.getLastClearedToken());

// afrer which there should be nothing to advance to:
assertNull(jp.nextToken());
assertNull(p.nextToken());

jp.close();
p.close();
}

@SuppressWarnings("resource")
public void testPojoReadingFailing()
throws IOException
public void testPojoReadingFailing() throws IOException
{
// regular factory can't do it, without a call to setCodec()
JsonFactory jf = new JsonFactory();
JsonFactory f = new JsonFactory();
try {
final String JSON = "{ \"x\" : 9 }";
JsonParser jp = jf.createParser(new StringReader(JSON));
Pojo p = jp.readValueAs(Pojo.class);
fail("Expected an exception: got "+p);
JsonParser p = f.createParser(new StringReader(JSON));
Pojo pojo = p.readValueAs(Pojo.class);
fail("Expected an exception: got "+pojo);
} catch (IllegalStateException e) {
verifyException(e, "No ObjectCodec defined");
}
}

// for [JACKSON-672]

public void testEscapingUsingMapper() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
ObjectMapper mapper = JsonMapper.builder()
.configure(JsonWriteFeature.ESCAPE_NON_ASCII, true)
.build();
mapper.writeValueAsString(String.valueOf((char) 257));
}
}
19 changes: 13 additions & 6 deletions src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
Expand Up @@ -8,10 +8,12 @@
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;

import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.*;

public class ObjectMapperTest extends BaseMapTest
Expand Down Expand Up @@ -56,19 +58,24 @@ static class NoCopyMapper extends ObjectMapper { }
/**********************************************************
*/

public void testFactorFeatures()
public void testFactoryFeatures()
{
assertTrue(MAPPER.isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES));
}

public void testGeneratorFeatures()
{
// and also for mapper
ObjectMapper mapper = new ObjectMapper();
assertFalse(mapper.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
assertTrue(mapper.isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
mapper.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM,
JsonGenerator.Feature.QUOTE_FIELD_NAMES);
JsonMapper mapper = new JsonMapper();
assertTrue(mapper.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
assertFalse(mapper.isEnabled(JsonWriteFeature.ESCAPE_NON_ASCII));
assertTrue(mapper.isEnabled(JsonWriteFeature.WRITE_NAN_AS_STRINGS));
mapper = JsonMapper.builder()
.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)
.disable(JsonWriteFeature.WRITE_NAN_AS_STRINGS)
.build();
assertFalse(mapper.isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
assertFalse(mapper.isEnabled(JsonWriteFeature.WRITE_NAN_AS_STRINGS));
}

public void testParserFeatures()
Expand Down
Expand Up @@ -11,6 +11,7 @@

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
Expand Down Expand Up @@ -90,12 +91,12 @@ public void testPrefetch() throws Exception
public void testObjectWriterFeatures() throws Exception
{
ObjectWriter writer = MAPPER.writer()
.without(JsonGenerator.Feature.QUOTE_FIELD_NAMES);
.without(JsonWriteFeature.QUOTE_FIELD_NAMES);
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a", 1);
assertEquals("{a:1}", writer.writeValueAsString(map));
// but can also reconfigure
assertEquals("{\"a\":1}", writer.with(JsonGenerator.Feature.QUOTE_FIELD_NAMES)
assertEquals("{\"a\":1}", writer.with(JsonWriteFeature.QUOTE_FIELD_NAMES)
.writeValueAsString(map));
}

Expand Down Expand Up @@ -260,9 +261,8 @@ public void testFeatureSettings() throws Exception
public void testGeneratorFeatures() throws Exception
{
ObjectWriter w = MAPPER.writer();
assertFalse(w.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
assertNotSame(w, w.with(JsonGenerator.Feature.ESCAPE_NON_ASCII));
assertNotSame(w, w.withFeatures(JsonGenerator.Feature.ESCAPE_NON_ASCII));
assertNotSame(w, w.with(JsonWriteFeature.ESCAPE_NON_ASCII));
assertNotSame(w, w.withFeatures(JsonWriteFeature.ESCAPE_NON_ASCII));

assertTrue(w.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
assertNotSame(w, w.without(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
Expand Down
Expand Up @@ -3,9 +3,8 @@
import java.util.Collections;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.*;

public class SerConfigTest extends BaseMapTest
Expand Down Expand Up @@ -51,17 +50,12 @@ public void testSerConfig() throws Exception
public void testGeneratorFeatures() throws Exception
{
SerializationConfig config = MAPPER.getSerializationConfig();
JsonFactory f = MAPPER.getFactory();
assertFalse(config.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII, f));
assertNotSame(config, config.with(JsonGenerator.Feature.ESCAPE_NON_ASCII));
SerializationConfig newConfig = config.withFeatures(JsonGenerator.Feature.ESCAPE_NON_ASCII,
JsonGenerator.Feature.IGNORE_UNKNOWN);
assertNotSame(config, config.with(JsonWriteFeature.ESCAPE_NON_ASCII));
SerializationConfig newConfig = config.withFeatures(JsonGenerator.Feature.IGNORE_UNKNOWN);
assertNotSame(config, newConfig);
assertTrue(newConfig.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII, f));

assertNotSame(config, config.without(JsonGenerator.Feature.ESCAPE_NON_ASCII));
assertNotSame(config, config.withoutFeatures(JsonGenerator.Feature.ESCAPE_NON_ASCII,
JsonGenerator.Feature.IGNORE_UNKNOWN));
assertNotSame(config, config.without(JsonWriteFeature.ESCAPE_NON_ASCII));
assertNotSame(config, config.withoutFeatures(JsonGenerator.Feature.IGNORE_UNKNOWN));
}

public void testFormatFeatures() throws Exception
Expand Down
Expand Up @@ -39,11 +39,11 @@ public void testBasicConfig() throws IOException
buf.setCodec(null);
assertNull(buf.getCodec());

assertFalse(buf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
buf.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
assertTrue(buf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
buf.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
assertFalse(buf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
assertFalse(buf.isEnabled(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN));
buf.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
assertTrue(buf.isEnabled(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN));
buf.disable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
assertFalse(buf.isEnabled(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN));

buf.close();
assertTrue(buf.isClosed());
Expand Down

0 comments on commit 5f45318

Please sign in to comment.