Alex Rivera | Logout

Google's Json Parsing Gson library: What's the difference between JsonElement and JsonObject?

Asked 2012-06-08T19:16:04.267
47
public abstract class JsonElement extends Object 

A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.

public final class JsonObject extends JsonElement 

A class representing an object type in Json. An object consists of name-value pairs where names are strings, and values are any other type of JsonElement. This allows for a creating a tree of JsonElements. The member elements of this object are maintained in order they were added.

Yay google!

Nevermind that question.

Edit
Report

1 Answer

6

JsonElement is the base type for all of the different specific types - it's a base class with common things that all nodes should implement. Logically, this makes a lot of sense.

The thing that all specific types inherits from JsonElement is a good thing for you, too. It allows you to test if an object is a Json node representation, simply by checking if it inherits from JsonElement.

The JsonElement class is available to you as a public class probably not because you'd want to subclass it, but because you'd want to test for its subclasses.

answered 2012-06-08T19:35:46.083

Your Answer