Skip to content

Commit 7b796a8

Browse files
committed
Fixed #72
1 parent 7a9d1d3 commit 7b796a8

File tree

3 files changed

+98
-29
lines changed

3 files changed

+98
-29
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Version: 2.2.1 (xx-May-2013)
33

44
Changes:
55

6+
#72: JsonFactory.copy() was not copying settings properly
7+
(reported by Christian S (squiddle@github))
68
- Moved VERSION/LICENSE contained in jars under META-INF/, to resolve
79
Android packaging (APK) issues
810

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,15 @@ public class JsonFactory
4242
java.io.Serializable // since 2.1 (for Android, mostly)
4343
{
4444
/**
45-
* Computed for Jackson 2.1.0 release
45+
* Computed for Jackson 2.2.0 release
4646
*/
4747
private static final long serialVersionUID = 8726401676402117450L;
4848

49-
/**
50-
* Name used to identify JSON format
51-
* (and returned by {@link #getFormatName()}
52-
*/
53-
public final static String FORMAT_NAME_JSON = "JSON";
54-
55-
/**
56-
* Bitfield (set of flags) of all factory features that are enabled by default.
57-
*/
58-
protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
59-
60-
/**
61-
* Bitfield (set of flags) of all parser features that are enabled
62-
* by default.
63-
*/
64-
protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
65-
66-
/**
67-
* Bitfield (set of flags) of all generator features that are enabled
68-
* by default.
49+
/*
50+
/**********************************************************
51+
/* Helper types
52+
/**********************************************************
6953
*/
70-
protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
71-
72-
private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
7354

7455
/**
7556
* Enumeration that defines all on/off features that can only be
@@ -138,7 +119,39 @@ private Feature(boolean defaultState)
138119
public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
139120

140121
public int getMask() { return (1 << ordinal()); }
141-
}
122+
}
123+
124+
/*
125+
/**********************************************************
126+
/* Constants
127+
/**********************************************************
128+
*/
129+
130+
/**
131+
* Name used to identify JSON format
132+
* (and returned by {@link #getFormatName()}
133+
*/
134+
public final static String FORMAT_NAME_JSON = "JSON";
135+
136+
/**
137+
* Bitfield (set of flags) of all factory features that are enabled by default.
138+
*/
139+
protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
140+
141+
/**
142+
* Bitfield (set of flags) of all parser features that are enabled
143+
* by default.
144+
*/
145+
protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
146+
147+
/**
148+
* Bitfield (set of flags) of all generator features that are enabled
149+
* by default.
150+
*/
151+
protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
152+
153+
private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
154+
142155
/*
143156
/**********************************************************
144157
/* Buffer, symbol table management
@@ -243,27 +256,53 @@ private Feature(boolean defaultState)
243256
* and this reuse only works within context of a single
244257
* factory instance.
245258
*/
246-
public JsonFactory() { this(null); }
259+
public JsonFactory() { this((ObjectCodec) null); }
247260

248261
public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
249262

263+
/**
264+
* Constructor used when copy()ing a factory instance.
265+
*
266+
* @since 2.2.1
267+
*/
268+
protected JsonFactory(JsonFactory src, ObjectCodec codec)
269+
{
270+
_objectCodec = null;
271+
_factoryFeatures = src._factoryFeatures;
272+
_parserFeatures = src._parserFeatures;
273+
_generatorFeatures = src._generatorFeatures;
274+
_characterEscapes = src._characterEscapes;
275+
_inputDecorator = src._inputDecorator;
276+
_outputDecorator = src._outputDecorator;
277+
_rootValueSeparator = src._rootValueSeparator;
278+
279+
/* 27-Apr-2013, tatu: How about symbol table; should we try to
280+
* reuse shared symbol tables? Could be more efficient that way;
281+
* although can slightly add to concurrency overhead.
282+
*/
283+
}
284+
250285
/**
251286
* Method for constructing a new {@link JsonFactory} that has
252287
* the same settings as this instance, but is otherwise
253288
* independent (i.e. nothing is actually shared, symbol tables
254289
* are separate).
255290
* Note that {@link ObjectCodec} reference is not copied but is
256291
* set to null; caller typically needs to set it after calling
257-
* this method.
292+
* this method. Reason for this is that the codec is used for
293+
* callbacks, and assumption is that there is strict 1-to-1
294+
* mapping between codec, factory. Caller has to, then, explicitly
295+
* set codec after making the copy.
258296
*
259297
* @since 2.1
260298
*/
261299
public JsonFactory copy()
262300
{
263301
_checkInvalidCopy(JsonFactory.class);
264-
return new JsonFactory(null);
302+
// as per above, do clear ObjectCodec
303+
return new JsonFactory(this, null);
265304
}
266-
305+
267306
/**
268307
* @since 2.1
269308
* @param exp
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fasterxml.jackson.core;
2+
3+
import com.fasterxml.jackson.test.BaseTest;
4+
5+
public class TestJsonFactory extends BaseTest
6+
{
7+
// #72
8+
public void testCopy() throws Exception
9+
{
10+
JsonFactory jf = new JsonFactory();
11+
// first, verify defaults
12+
assertTrue(jf.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
13+
assertFalse(jf.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
14+
assertFalse(jf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
15+
jf.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
16+
jf.enable(JsonParser.Feature.ALLOW_COMMENTS);
17+
jf.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
18+
// then change, verify that changes "stick"
19+
assertFalse(jf.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
20+
assertTrue(jf.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
21+
assertTrue(jf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
22+
23+
JsonFactory jf2 = jf.copy();
24+
assertFalse(jf2.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
25+
assertTrue(jf.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
26+
assertTrue(jf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
27+
}
28+
}

0 commit comments

Comments
 (0)