Skip to content

Commit a511f27

Browse files
mkargquipsy-karg
authored andcommitted
Enhancement [JOHNZON-278] Return constant instead of new instance of empty block / empty array
Even in these days of modern Java, reducing memory consumption, GC stress and memory fragmentation, are beneficial side aspects. If makes no sense to create new empty instances of JsonArray and JsonObject again and again. For this case, JSON-P provides special constants. It makes sense to return these constants always. Signed-off-by: Markus KARG <markus@headcrashing.eu>
1 parent d6723b7 commit a511f27

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public JsonArrayBuilder add(final JsonArrayBuilder builder) {
298298
addValue(builder.build());
299299
return this;
300300
}
301-
301+
302302
private void setValue(int idx, JsonValue value) {
303303
if (value == null || tmpList == null) {
304304
throw npe();
@@ -333,7 +333,7 @@ private void addValue(int idx, JsonValue value) {
333333
@Override
334334
public JsonArray build() {
335335
if(tmpList == null) {
336-
return new JsonArrayImpl(Collections.emptyList(), bufferProvider);
336+
return JsonValue.EMPTY_JSON_ARRAY;
337337
}
338338
return new JsonArrayImpl(Collections.unmodifiableList(tmpList), bufferProvider);
339339
}

johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectBuilderImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,16 @@ private void putValue(String name, JsonValue value){
176176
if(name == null || value == null) {
177177
throw new NullPointerException("name or value/builder must not be null");
178178
}
179-
179+
180180
attributeMap.put(name, value);
181181
}
182-
182+
183183

184184
@Override
185185
public JsonObject build() {
186-
186+
187187
if(attributeMap == null || attributeMap.isEmpty()) {
188-
return new JsonObjectImpl(Collections.EMPTY_MAP, bufferProvider);
188+
return JsonValue.EMPTY_JSON_OBJECT;
189189
} else {
190190
Map<String, JsonValue> dump = (Collections.unmodifiableMap(attributeMap));
191191
return new JsonObjectImpl(dump, bufferProvider);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<url>http://johnzon.apache.org</url>
3737

3838
<properties>
39-
<geronimo-jsonp.version>1.2</geronimo-jsonp.version>
39+
<geronimo-jsonp.version>1.3-SNAPSHOT</geronimo-jsonp.version>
4040
<geronimo-jsonb.version>1.2</geronimo-jsonb.version>
4141
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4242
<johnzon.site.url>https://svn.apache.org/repos/asf/johnzon/site/publish/</johnzon.site.url>

0 commit comments

Comments
 (0)