Skip to content

Commit 0987598

Browse files
committed
Fix #2176
1 parent 36ff317 commit 0987598

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ Versions: 3.x (for earlier see VERSION-2.x)
3535
#1994: Limit size of `SerializerCache`, auto-flush on exceeding
3636
#1995: Limit size of `DeserializerCache`, auto-flush on exceeding
3737
#2040: Remove `JsonSerializer.isEmpty()` from 3.0
38+
#2176: Add `JsonMapper.shared()` static method
3839
- Remove `MappingJsonFactory`
3940
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)

src/main/java/com/fasterxml/jackson/databind/json/JsonMapper.java

+38-6
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ protected Object readResolve() {
113113
}
114114

115115
/*
116-
/**********************************************************
116+
/**********************************************************************
117117
/* Life-cycle, constructors
118-
/**********************************************************
118+
/**********************************************************************
119119
*/
120120

121121
public JsonMapper() {
@@ -131,9 +131,9 @@ public JsonMapper(Builder b) {
131131
}
132132

133133
/*
134-
/**********************************************************
134+
/**********************************************************************
135135
/* Life-cycle, builders
136-
/**********************************************************
136+
/**********************************************************************
137137
*/
138138

139139
public static JsonMapper.Builder builder() {
@@ -151,9 +151,25 @@ public JsonMapper.Builder rebuild() {
151151
}
152152

153153
/*
154-
/**********************************************************
154+
/**********************************************************************
155+
/* Life-cycle, shared "vanilla" (default configuration) instance
156+
/**********************************************************************
157+
*/
158+
159+
/**
160+
* Accessor method for getting globally shared "default" {@link JsonMapper}
161+
* instance: one that has default configuration, no modules registered, no
162+
* config overrides. Usable mostly when dealing "untyped" or Tree-style
163+
* content reading and writing.
164+
*/
165+
public static JsonMapper shared() {
166+
return SharedWrapper.wrapped();
167+
}
168+
169+
/*
170+
/**********************************************************************
155171
/* Standard method overrides
156-
/**********************************************************
172+
/**********************************************************************
157173
*/
158174

159175
@Override
@@ -179,4 +195,20 @@ public boolean isEnabled(JsonReadFeature f) {
179195
public boolean isEnabled(JsonWriteFeature f) {
180196
return _serializationConfig.hasFormatFeature(f);
181197
}
198+
199+
/*
200+
/**********************************************************
201+
/* Helper class(es)
202+
/**********************************************************
203+
*/
204+
205+
/**
206+
* Helper class to contain dynamically constructed "shared" instance of
207+
* mapper, should one be needed via {@link #shared}.
208+
*/
209+
private final static class SharedWrapper {
210+
private final static JsonMapper MAPPER = JsonMapper.builder().build();
211+
212+
public static JsonMapper wrapped() { return MAPPER; }
213+
}
182214
}

src/main/java/com/fasterxml/jackson/databind/node/BaseJsonNode.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.JsonSerializable;
88
import com.fasterxml.jackson.databind.SerializerProvider;
9+
import com.fasterxml.jackson.databind.json.JsonMapper;
910
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1011

1112
/**
@@ -111,11 +112,21 @@ public abstract void serializeWithType(JsonGenerator jgen, SerializerProvider pr
111112

112113
@Override
113114
public String toString() {
114-
return InternalNodeMapper.nodeToString(this);
115+
try {
116+
return JsonMapper.shared().writeValueAsString(this);
117+
} catch (IOException e) { // should never occur
118+
throw new RuntimeException(e);
119+
}
115120
}
116121

117122
@Override
118123
public String toPrettyString() {
119-
return InternalNodeMapper.nodeToPrettyString(this);
124+
try {
125+
return JsonMapper.shared()
126+
.writerWithDefaultPrettyPrinter()
127+
.writeValueAsString(this);
128+
} catch (IOException e) { // should never occur
129+
throw new RuntimeException(e);
130+
}
120131
}
121132
}

src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.java

-37
This file was deleted.

0 commit comments

Comments
 (0)