Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate JsonElement constructor #1761

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions gson/src/main/java/com/google/gson/JsonArray.java
Expand Up @@ -36,10 +36,19 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
/**
* Creates an empty JsonArray.
*/
@SuppressWarnings("deprecation") // JsonElement constructor
Marcono1234 marked this conversation as resolved.
Show resolved Hide resolved
public JsonArray() {
elements = new ArrayList<JsonElement>();
}


/**
* Creates an empty JsonArray with the desired initial capacity.
*
* @param capacity initial capacity.
* @throws IllegalArgumentException if the {@code capacity} is
* negative
*/
@SuppressWarnings("deprecation") // JsonElement constructor
public JsonArray(int capacity) {
elements = new ArrayList<JsonElement>(capacity);
}
Expand Down Expand Up @@ -171,7 +180,7 @@ public boolean contains(JsonElement element) {
public int size() {
return elements.size();
}

/**
* Returns true if the array is empty
*
Expand Down
9 changes: 9 additions & 0 deletions gson/src/main/java/com/google/gson/JsonElement.java
Expand Up @@ -31,6 +31,15 @@
* @author Joel Leitch
*/
public abstract class JsonElement {
/**
* @deprecated Creating custom {@code JsonElement} subclasses is highly discouraged
* and can lead to undefined behavior.<br>
* This constructor is only kept for backward compatibility.
*/
@Deprecated
public JsonElement() {
}

/**
* Returns a deep copy of this element. Immutable elements like primitives
* and nulls are not copied.
Expand Down
5 changes: 3 additions & 2 deletions gson/src/main/java/com/google/gson/JsonNull.java
Expand Up @@ -25,15 +25,16 @@
*/
public final class JsonNull extends JsonElement {
/**
* singleton for JsonNull
* Singleton for JsonNull
*
* @since 1.8
*/
public static final JsonNull INSTANCE = new JsonNull();

/**
* Creates a new JsonNull object.
* Deprecated since Gson version 1.8. Use {@link #INSTANCE} instead
*
* @deprecated Deprecated since Gson version 1.8. Use {@link #INSTANCE} instead
*/
@Deprecated
public JsonNull() {
Expand Down
7 changes: 7 additions & 0 deletions gson/src/main/java/com/google/gson/JsonObject.java
Expand Up @@ -33,6 +33,13 @@ public final class JsonObject extends JsonElement {
private final LinkedTreeMap<String, JsonElement> members =
new LinkedTreeMap<String, JsonElement>();

/**
* Creates an empty JsonObject.
*/
@SuppressWarnings("deprecation") // JsonElement constructor
public JsonObject() {
}

/**
* Creates a deep copy of this element and all its children
* @since 2.8.2
Expand Down
4 changes: 4 additions & 0 deletions gson/src/main/java/com/google/gson/JsonPrimitive.java
Expand Up @@ -39,6 +39,7 @@ public final class JsonPrimitive extends JsonElement {
*
* @param bool the value to create the primitive with.
*/
@SuppressWarnings("deprecation") // JsonElement constructor
public JsonPrimitive(Boolean bool) {
value = $Gson$Preconditions.checkNotNull(bool);
}
Expand All @@ -48,6 +49,7 @@ public JsonPrimitive(Boolean bool) {
*
* @param number the value to create the primitive with.
*/
@SuppressWarnings("deprecation") // JsonElement constructor
public JsonPrimitive(Number number) {
value = $Gson$Preconditions.checkNotNull(number);
}
Expand All @@ -57,6 +59,7 @@ public JsonPrimitive(Number number) {
*
* @param string the value to create the primitive with.
*/
@SuppressWarnings("deprecation") // JsonElement constructor
public JsonPrimitive(String string) {
value = $Gson$Preconditions.checkNotNull(string);
}
Expand All @@ -67,6 +70,7 @@ public JsonPrimitive(String string) {
*
* @param c the value to create the primitive with.
*/
@SuppressWarnings("deprecation") // JsonElement constructor
public JsonPrimitive(Character c) {
// convert characters to strings since in JSON, characters are represented as a single
// character string
Expand Down
Expand Up @@ -23,11 +23,12 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.MalformedJsonException;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Arrays;

/**
* This reader walks the elements of a JsonElement as if it was coming from a
Expand Down Expand Up @@ -143,7 +144,7 @@ public JsonTreeReader(JsonElement element) {
} else if (o == SENTINEL_CLOSED) {
throw new IllegalStateException("JsonReader is closed");
} else {
throw new AssertionError();
throw new MalformedJsonException("Custom JsonElement subclass " + o.getClass().getName() + " is not supported");
}
}

Expand Down
Expand Up @@ -16,9 +16,11 @@
package com.google.gson.internal.bind;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.MalformedJsonException;
import java.io.IOException;
import junit.framework.TestCase;

Expand Down Expand Up @@ -54,4 +56,28 @@ public void testHasNext_endOfDocument() throws IOException {
reader.endObject();
assertFalse(reader.hasNext());
}

public void testCustomJsonElementSubclass() throws IOException {
@SuppressWarnings("deprecation") // JsonElement constructor
class CustomSubclass extends JsonElement {
@Override
public JsonElement deepCopy() {
return this;
}
}

JsonArray array = new JsonArray();
array.add(new CustomSubclass());

JsonTreeReader reader = new JsonTreeReader(array);
reader.beginArray();
try {
// Should fail due to custom JsonElement subclass
reader.peek();
fail();
} catch (MalformedJsonException expected) {
assertEquals("Custom JsonElement subclass com.google.gson.internal.bind.JsonTreeReaderTest$1CustomSubclass is not supported",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would write:

assertEquals("Custom JsonElement subclass " + CustomSubclass.class.getName() + " is not supported", ...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, that hardcoded generated name JsonTreeReaderTest$1CustomSubclass would likely cause problems in the future.

expected.getMessage());
}
}
}
Expand Up @@ -21,9 +21,6 @@
import com.google.gson.internal.$Gson$Types;
import junit.framework.TestCase;

import java.io.PrintStream;
import java.lang.ref.WeakReference;

/**
* Test fixes for infinite recursion on {@link $Gson$Types#resolve(java.lang.reflect.Type, Class,
* java.lang.reflect.Type)}, described at <a href="https://github.com/google/gson/issues/440">Issue #440</a>
Expand Down