Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON JsonElement.getAsString vs. JsonElement.toString?

Tags:

What is the difference between JsonElement#getAsString() vs. JsonElement#toString()?

Are there situations where one should be used over the other?

like image 350
JDOaktown Avatar asked Dec 06 '15 18:12

JDOaktown


People also ask

Does GSON use toString?

Both Gson. toJson() and JSONObjet. toString() have the same effect. They map your JSON object and return to you as a String.

What is Jsonelement in GSON?

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

What is the difference between getasstring() and tostring() in JavaScript?

As you can see, the output is in some cases quite similar (or even equals), but it differs in some other cases. getAsString () is only defined for primitive types (and arrays containing only a single primitive element) and throws an exception if called e.g. on an object. toString () will work on all types of JsonElement.

How to get if an element is a JSON object?

convenience method to get this element as a JsonObject. If the element is of some other type, a Ille convenience method to get this element as a JsonArray. If the element is of some other type, a Illeg convenience method to get this element as a primitive integer value. provides check for verifying if this element is a Json object or not.

How do Gson and Jackson compare for JSON data binding in Java?

In this article, we'll compare the Gson and Jackson APIs for serializing and deserializing JSON data to Java objects and vice-versa. Gson and Jackson are complete libraries offering JSON data-binding support for Java. Each are actively developed open-source projects which offer to handle of complex data types and support for Java Generics.

What is the use of toString method in Java?

This method accesses and returns a property of the element, i.e. the value of the element as a java String object. Returns a String representation of this element. This method is the "standard" java toString method, i.e. returns a human readable representation of the element itself.


Video Answer


1 Answers

Assuming that you are referring to JsonElement:

getAsString()

convenience method to get this element as a string value.

This method accesses and returns a property of the element, i.e. the value of the element as a java String object.

toString()

Returns a String representation of this element.

This method is the "standard" java toString method, i.e. returns a human readable representation of the element itself.

For better understanding, let me give you an example:

import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive;  public class GsonTest {      public static void main(String[] args) {         JsonElement jsonElement = new JsonPrimitive("foo");          System.out.println(jsonElement.toString());         System.out.println(jsonElement.getAsString());          jsonElement = new JsonPrimitive(42);          System.out.println(jsonElement.toString());         System.out.println(jsonElement.getAsString());          jsonElement = new JsonPrimitive(true);          System.out.println(jsonElement.toString());         System.out.println(jsonElement.getAsString());          jsonElement = new JsonObject();         ((JsonObject) jsonElement).addProperty("foo", "bar");         ((JsonObject) jsonElement).addProperty("foo2", 42);          System.out.println(jsonElement.toString());         System.out.println(jsonElement.getAsString());     } } 

Output:

"foo" foo 42 42 true true {"foo":"bar","foo2":42} Exception in thread "main" java.lang.UnsupportedOperationException: JsonObject     at com.google.gson.JsonElement.getAsString(JsonElement.java:185) 

As you can see, the output is in some cases quite similar (or even equals), but it differs in some other cases. getAsString() is only defined for primitive types (and arrays containing only a single primitive element) and throws an exception if called e.g. on an object. toString() will work on all types of JsonElement.

Now when should you use which method?

  • If you want to print out debug information, use toString()
  • If you know that you are dealing with a primitive type and you want to display or write the value somewhere, use getAsString()
  • If you don't know the type or if you want to work with the value (i.e. do calculations), use neither. Instead check for the type of the element and use the appropriate method.
like image 144
Marvin Avatar answered Sep 18 '22 13:09

Marvin