Skip to content

Commit 0b6ea7c

Browse files
authored
Merge branch '2.13' into jsonproperty-enum
2 parents c81cc0f + bbab957 commit 0b6ea7c

File tree

17 files changed

+300
-64
lines changed

17 files changed

+300
-64
lines changed

jr-annotation-support/src/main/java/com/fasterxml/jackson/jr/annotationsupport/AnnotationBasedIntrospector.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ public class AnnotationBasedIntrospector
3939
// // // State only for deserialization:
4040

4141
protected Set<String> _ignorableNames;
42+
protected int _features;
4243

4344
protected AnnotationBasedIntrospector(Class<?> type, boolean serialization,
44-
JsonAutoDetect.Value visibility) {
45+
JsonAutoDetect.Value visibility, int features) {
4546
_type = type;
4647
_forSerialization = serialization;
4748
_ignorableNames = serialization ? null : new HashSet<String>();
49+
_features = features;
4850

4951
// First things first: find possible `@JsonAutoDetect` to override
5052
// default visibility settings
@@ -58,13 +60,13 @@ protected AnnotationBasedIntrospector(Class<?> type, boolean serialization,
5860

5961
public static POJODefinition pojoDefinitionForDeserialization(JSONReader r,
6062
Class<?> pojoType, JsonAutoDetect.Value visibility) {
61-
return new AnnotationBasedIntrospector(pojoType, false, visibility)
63+
return new AnnotationBasedIntrospector(pojoType, false, visibility, r.features())
6264
.introspectDefinition();
6365
}
6466

6567
public static POJODefinition pojoDefinitionForSerialization(JSONWriter w,
6668
Class<?> pojoType, JsonAutoDetect.Value visibility) {
67-
return new AnnotationBasedIntrospector(pojoType, true, visibility)
69+
return new AnnotationBasedIntrospector(pojoType, true, visibility, w.features())
6870
.introspectDefinition();
6971
}
7072

@@ -235,9 +237,9 @@ protected void _findFields(final Class<?> currType)
235237

236238
// then get fields from within class itself
237239
for (Field f : currType.getDeclaredFields()) {
238-
// Does not include static fields, but there are couple of things we do
239-
// not include regardless:
240-
if (f.isSynthetic()) {
240+
// skip static fields and synthetic fields except for enum constants
241+
if ((JSON.Feature.INCLUDE_STATIC_FIELDS.isDisabled(_features) && Modifier.isStatic(f.getModifiers())
242+
&& !f.isEnumConstant()) || f.isSynthetic()) {
241243
continue;
242244
}
243245
// otherwise, first things first; explicit ignoral?
@@ -284,7 +286,7 @@ protected void _findMethods(final Class<?> currType)
284286
final int flags = m.getModifiers();
285287
// 13-Jun-2015, tatu: Skip synthetic, bridge methods altogether, for now
286288
// at least (add more complex handling only if absolutely necessary)
287-
if (Modifier.isStatic(flags)
289+
if ((JSON.Feature.INCLUDE_STATIC_FIELDS.isDisabled(_features) && Modifier.isStatic(flags))
288290
|| m.isSynthetic() || m.isBridge()) {
289291
continue;
290292
}
@@ -410,9 +412,10 @@ protected void _checkSetterMethod(Method m)
410412
*/
411413

412414
protected boolean _isFieldVisible(Field f) {
413-
// Consider transient to be non-visible
415+
// Consider transient and static-final to be non-visible
414416
// TODO: (maybe?) final
415-
return !Modifier.isTransient(f.getModifiers())
417+
return !(Modifier.isFinal(f.getModifiers()) && Modifier.isStatic(f.getModifiers()))
418+
&& !Modifier.isTransient(f.getModifiers())
416419
&& _visibility.getFieldVisibility().isVisible(f);
417420
}
418421

jr-annotation-support/src/test/java/com/fasterxml/jackson/jr/annotationsupport/BasicIgnoralTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
public class BasicIgnoralTest extends ASTestBase
88
{
99
static class XY {
10+
public static int DEFAULT = 123;
11+
public static final int DEFAULT_FINAL = 123;
1012
@JsonIgnore
1113
public int x;
1214
public int y;
@@ -42,13 +44,16 @@ public XYZ(int x, int y, int z) {
4244
}
4345

4446
private final JSON JSON_WITH_ANNO = jsonWithAnnotationSupport();
47+
private final JSON JSON_WITH_ANNO_WITH_STATIC =
48+
JSON.builder().register(JacksonAnnotationExtension.std).enable(JSON.Feature.INCLUDE_STATIC_FIELDS).build();
49+
private final JSON JSON_WITH_STATIC = JSON.builder().enable(JSON.Feature.INCLUDE_STATIC_FIELDS).build();
4550

4651
/*
4752
/**********************************************************************
4853
/* Tests for basic @JsonIgnore
4954
/**********************************************************************
5055
*/
51-
56+
5257
public void testPropertyIgnoralOnSerialize() throws Exception
5358
{
5459
final XY input = new XY(1, 2);
@@ -60,26 +65,41 @@ public void testPropertyIgnoralOnSerialize() throws Exception
6065

6166
// and ensure no leakage to default one:
6267
assertEquals(a2q("{'x':1,'y':2}"), JSON.std.asString(input));
68+
69+
// Verify serialization of static fields when the INCLUDE_STATIC_FIELDS option is enabled
70+
assertEquals(a2q("{'DEFAULT':123,'x':1,'y':2}"), JSON_WITH_STATIC.asString(input));
71+
assertEquals(a2q("{'DEFAULT':123,'y':2}"), JSON_WITH_ANNO_WITH_STATIC.asString(input));
6372
}
6473

6574
public void testPropertyIgnoralOnDeserialize() throws Exception
6675
{
67-
final String json = a2q("{'x':1,'y':2}");
76+
final String json = a2q("{'DEFAULT':125,'x':1,'y':2}");
6877

6978
// default: no filtering by ignorals
7079
XY result = JSON.std.beanFrom(XY.class, json);
7180
assertEquals(1, result.x);
7281
assertEquals(2, result.y);
82+
assertEquals(XY.DEFAULT_FINAL, XY.DEFAULT);
7383

7484
// but with ignore, should skip
7585
result = JSON_WITH_ANNO.beanFrom(XY.class, json);
7686
assertEquals(0, result.x);
7787
assertEquals(2, result.y);
88+
assertEquals(XY.DEFAULT_FINAL, XY.DEFAULT);
7889

7990
// and once again verify non-stickiness
8091
result = JSON.std.beanFrom(XY.class, json);
8192
assertEquals(1, result.x);
8293
assertEquals(2, result.y);
94+
assertEquals(XY.DEFAULT_FINAL, XY.DEFAULT);
95+
96+
// Verify setting static field from serialized data when the INCLUDE_STATIC_FIELDS option is enabled
97+
JSON_WITH_STATIC.beanFrom(XY.class, json);
98+
assertEquals(125, XY.DEFAULT);
99+
XY.DEFAULT = XY.DEFAULT_FINAL;
100+
JSON_WITH_ANNO_WITH_STATIC.beanFrom(XY.class, json);
101+
assertEquals(125, XY.DEFAULT);
102+
XY.DEFAULT = XY.DEFAULT_FINAL;
83103
}
84104

85105
public void testPropertyIgnoreWithUnknown() throws Exception

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/JSON.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* <li>{@link URL}</li>
3232
* <li>{@link File}</li>
3333
* </ul>
34-
*
34+
*
3535
*/
3636
@SuppressWarnings("resource")
3737
public class JSON implements Versioned
@@ -167,7 +167,7 @@ public enum Feature
167167
* @since 2.7
168168
*/
169169
WRITE_DATES_AS_TIMESTAMP(false),
170-
170+
171171
/**
172172
* Feature that can be enabled to use "pretty-printing", basic indentation
173173
* to make resulting JSON easier to read by humans by adding white space
@@ -241,7 +241,7 @@ public enum Feature
241241
* so that all Bean properties are serialized.
242242
*/
243243
WRITE_READONLY_BEAN_PROPERTIES(true, true),
244-
244+
245245
/**
246246
* Feature that determines whether access to {@link java.lang.reflect.Method}s and
247247
* {@link java.lang.reflect.Constructor}s that are used with dynamically
@@ -260,7 +260,7 @@ public enum Feature
260260
* @since 2.5
261261
*/
262262
USE_IS_GETTERS(true, true),
263-
263+
264264
/**
265265
* Feature that enables use of public fields instead of setters and getters,
266266
* in cases where no setter/getter is available.
@@ -271,6 +271,16 @@ public enum Feature
271271
* @since 2.8 (enabled by default since 2.10)
272272
*/
273273
USE_FIELDS(true, true),
274+
275+
/**
276+
* Feature that enables serialization and deserialization of non-final static fields.
277+
*<p>
278+
* Feature did not exist, but was implicitly <b>enabled</b> by default. <b>disabled</b> for
279+
* 2.13).
280+
*
281+
* @since 2.13
282+
*/
283+
INCLUDE_STATIC_FIELDS(false, true),
274284
;
275285

276286
/*
@@ -422,7 +432,7 @@ public static class Builder {
422432
protected PrettyPrinter _prettyPrinter;
423433

424434
// Configuration, helper objects
425-
435+
426436
protected final JsonFactory _streamFactory;
427437
protected TreeCodec _treeCodec;
428438

@@ -502,7 +512,7 @@ public Builder disable(Feature ... features)
502512
}
503513

504514
// // // Mutators, helper objects
505-
515+
506516
/**
507517
* Method for specifying {@link PrettyPrinter} {@link JSON} to be built
508518
* should use on serialization.
@@ -732,7 +742,7 @@ protected JSONWriter _defaultWriter() {
732742
public ObjectCodec asCodec() {
733743
return new JSONAsObjectCodec(this);
734744
}
735-
745+
736746
/*
737747
/**********************************************************************
738748
/* Versioned
@@ -785,7 +795,7 @@ public JSON with(Feature feature, boolean state)
785795
}
786796
return _with(f);
787797
}
788-
798+
789799
/**
790800
* Mutant factory for constructing an instance with specified features
791801
* enabled.
@@ -825,7 +835,7 @@ protected final JSON _with(int features)
825835
// changes
826836
JSONReader r = _reader.withCacheCheck(features);
827837
JSONWriter w = _writer.withCacheCheck(features);
828-
838+
829839
return _with(features, _jsonFactory, _treeCodec,
830840
r, w, _prettyPrinter);
831841
}
@@ -839,14 +849,14 @@ protected final JSON _with(int features)
839849
/**
840850
* Mutant factory method for constructing new instance with specified {@link JsonFactory}
841851
* if different from currently configured one (if not, return {@code this} as-is)
842-
*
852+
*
843853
* @param f Jackson core format factory to use for low-level decoding/encoding
844854
*
845855
* @return New instance with specified factory (if not same as currently configured);
846856
* {@code this} otherwise.
847857
*
848858
* @deprecated Since 2.11 should not try changing underlying stream factory but create
849-
* a new instance if necessary: method will be removed from 3.0 at latest
859+
* a new instance if necessary: method will be removed from 3.0 at latest
850860
*/
851861
@Deprecated
852862
public JSON with(JsonFactory f) {
@@ -966,7 +976,7 @@ public JSON with(ReaderWriterProvider rwp) {
966976
/* Methods sub-classes must override
967977
/**********************************************************************
968978
*/
969-
979+
970980
protected JSON _with(int features,
971981
JsonFactory jsonF, TreeCodec trees,
972982
JSONReader reader, JSONWriter writer,
@@ -992,7 +1002,7 @@ public JsonFactory getStreamingFactory() {
9921002
public final boolean isEnabled(Feature f) {
9931003
return (f.mask() & _features) != 0;
9941004
}
995-
1005+
9961006
/*
9971007
/**********************************************************************
9981008
/* Public factory methods for parsers, generators
@@ -1122,7 +1132,7 @@ public CollectionComposer<?,List<Object>> composeList() {
11221132
public <C extends Collection<Object>> CollectionComposer<?,C> composeCollection(C collection) {
11231133
return new CollectionComposer<ComposerBase,C>(collection);
11241134
}
1125-
1135+
11261136
public MapComposer<?> composeMap() {
11271137
return composeMap(new LinkedHashMap<String,Object>());
11281138
}
@@ -1294,7 +1304,7 @@ public <T> T beanFrom(Class<T> type, Object source) throws IOException, JSONObje
12941304
return _closeWithError(p, e);
12951305
}
12961306
}
1297-
1307+
12981308
/**
12991309
* Read method that will take given JSON Source (of one of supported types),
13001310
* read contents and map it to one of simple mappings ({@link java.util.Map}
@@ -1476,7 +1486,7 @@ public <T extends TreeNode> T createArrayNode() {
14761486
}
14771487
return (T) _treeCodec.createArrayNode();
14781488
}
1479-
1489+
14801490
/**
14811491
* Convenience method, equivalent to:
14821492
*<pre>
@@ -1527,7 +1537,7 @@ protected JSONWriter _writerForOperation(JsonGenerator gen) {
15271537
/* Internal methods, reading
15281538
/**********************************************************************
15291539
*/
1530-
1540+
15311541
protected JSONReader _readerForOperation(JsonParser p) {
15321542
return _reader.perOperationInstance(_features, _valueReaderLocator, _treeCodec, p);
15331543
}
@@ -1585,7 +1595,7 @@ protected JsonParser _initForReading(JsonParser p) throws IOException
15851595
/* Internal methods, other
15861596
/**********************************************************************
15871597
*/
1588-
1598+
15891599
protected JsonGenerator _config(JsonGenerator g)
15901600
{
15911601
// First, possible pretty printing
@@ -1629,7 +1639,7 @@ protected <T> T _throw(Exception e) throws IOException {
16291639
}
16301640
throw new IOException(e); // should never occur
16311641
}
1632-
1642+
16331643
protected void _noTreeCodec(String msg) {
16341644
throw new IllegalStateException("JSON instance does not have configured `TreeCodec` to "+msg);
16351645
}
@@ -1641,7 +1651,7 @@ protected void _noTreeCodec(String msg) {
16411651
*/
16421652

16431653
/**
1644-
* Extension context implementation used when
1654+
* Extension context implementation used when
16451655
*/
16461656
private static class ExtContextImpl extends ExtensionContext {
16471657
final Builder _builder;

0 commit comments

Comments
 (0)