Tabnine Logo
Gson.toJson
Code IndexAdd Tabnine to your IDE (free)

How to use
toJson
method
in
com.google.gson.Gson

Best Java code snippets using com.google.gson.Gson.toJson (Showing top 20 results out of 17,883)

Refine searchRefine arrow

  • Gson.<init>
  • GsonBuilder.<init>
  • Gson.fromJson
  • GsonBuilder.create
  • PrintStream.println
origin: stackoverflow.com

 Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(myObject); // myObject - instance of MyObject
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
origin: apache/storm

public String toJson() {
  String json = gson.toJson(this);
  System.out.println(json);   // TODO log
  return json;
}
origin: stackoverflow.com

 Gson g = new Gson();

Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(person.name); //John

System.out.println(g.toJson(person)); // {"name":"John"}
origin: stackoverflow.com

 Map<String, String> myMap = new HashMap<String, String>();
myMap.put("one", "hello");
myMap.put("two", "world");

Gson gson = new GsonBuilder().create();
String json = gson.toJson(myMap);

System.out.println(json);

Type typeOfHashMap = new TypeToken<Map<String, String>>() { }.getType();
Map<String, String> newMap = gson.fromJson(json, typeOfHashMap); // This type must match TypeToken
System.out.println(newMap.get("one"));
System.out.println(newMap.get("two"));
origin: Vedenin/useful-java-links

  /**
   * Example to writeJson using TreeModel
   */
  private static void writeJson() throws IOException {
    JsonObject rootObject = new JsonObject();
    rootObject.addProperty("message", "Hi");
    JsonObject childObject = new JsonObject();
    childObject.addProperty("name", "World!");
    rootObject.add("place", childObject);

    Gson gson = new Gson();
    String json = gson.toJson(rootObject);
    System.out.println(json); // print "{"message":"Hi","place":{"name":"World!"}}"
  }
}
origin: stackoverflow.com

Gson gson = new GsonBuilder()
   .setExclusionStrategies(new TestExclStrat())
   //.serializeNulls() <-- uncomment to serialize NULL fields as well
   .create();
 Student src = new Student();
 String json = gson.toJson(src);
 System.out.println(json);
origin: stackoverflow.com

 //from object to JSON 
Gson gson = new Gson();
gson.toJson(yourObject);

// from JSON to object 
yourObject o = gson.fromJson(JSONString,yourObject.class);
origin: ctripcorp/apollo

private String mockLongPollBody(String notificationsStr) {
 List<ApolloConfigNotification> oldNotifications = gson.fromJson(notificationsStr, notificationType);
 List<ApolloConfigNotification> newNotifications = new ArrayList<>();
 for (ApolloConfigNotification notification : oldNotifications) {
  newNotifications
    .add(new ApolloConfigNotification(notification.getNamespaceName(), notification.getNotificationId() + 1));
 }
 return gson.toJson(newNotifications);
}
origin: gocd/gocd

@Override
public String requestMessageForNotifyPluginSettingsChange(Map<String, String> pluginSettings) {
  return new GsonBuilder().create().toJson(pluginSettings);
}
origin: stackoverflow.com

 Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
SomeContainerObject original = new SomeContainerObject(new DateTime());

String json = gson.toJson(original);
SomeContainerObject reconstituted = gson.fromJson(json, SomeContainerObject.class);
origin: stackoverflow.com

 Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);
origin: stackoverflow.com

String jsonVal0 = "{\"id\": 5382, \"user\": \"Mary\" }";
 String jsonVal1 = "{\"id\": 2341, \"person\": \"Bob\"}";
 final GsonBuilder gsonBuilder = new GsonBuilder();
 gsonBuilder.registerTypeAdapter(MyClass.class, new MyClassTypeAdapter());
 final Gson gson = gsonBuilder.create();
 MyClass myClassInstance0 = gson.fromJson(jsonVal0, MyClass.class);
 MyClass myClassInstance1 = gson.fromJson(jsonVal1, MyClass.class);
 System.out.println("jsonVal0 :" + gson.toJson(myClassInstance0));
 // output: jsonVal0 :{"id":5382,"name":"Mary"}
 System.out.println("jsonVal1 :" + gson.toJson(myClassInstance1));
 // output: jsonVal1 :{"id":2341,"name":"Bob"}
origin: stackoverflow.com

 import java.io.FileReader;
import java.util.ArrayList;

import com.google.gson.Gson;

public class Foo
{
 public static void main(String[] args) throws Exception
 {
  Gson gson = new Gson();
  TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class);
  System.out.println(gson.toJson(myTypes));
 }
}

class TypeDTO
{
 int id;
 String name;
 ArrayList<ItemDTO> items;
}

class ItemDTO
{
 int id;
 String name;
 Boolean valid;
}
origin: stackoverflow.com

 Gson gson = new Gson();

String inputString= gson.toJson(inputArray);

System.out.println("inputString= " + inputString);
origin: stackoverflow.com

Gson gson = new GsonBuilder()
   .setExclusionStrategies(new TestExclStrat("in.naishe.test.Country.name"))
   //.serializeNulls()
   .create();
 Student src = new Student();
 String json = gson.toJson(src);
 System.out.println(json);
origin: stackoverflow.com

public static void main(String[] args) {
   ObixBaseObj lobbyObj = new ObixBaseObj();
   lobbyObj.setIs("obix:Lobby");
   ObixOp batchOp = new ObixOp();
   batchOp.setName("batch");
   batchOp.setIn("obix:BatchIn");
   batchOp.setOut("obix:BatchOut");
   lobbyObj.addChild(batchOp);
   Gson gson = GsonUtils.getGson();
   System.out.println(gson.toJson(lobbyObj));
 }
origin: stackoverflow.com

 Type listType = new TypeToken<LinkedList>() {}.getType();
List target = new LinkedList();
target.add("blah");

Gson gson = new Gson();
String json = gson.toJson(target, listType);
List target2 = gson.fromJson(json, listType);
origin: gocd/gocd

@Test
public void shouldHandlePolymorphismWhenDeserializingAntTask()
{
  CRTask value = antTask;
  String json = gson.toJson(value);
  CRBuildTask deserializedValue = (CRBuildTask)gson.fromJson(json,CRTask.class);
  assertThat("Deserialized value should equal to value before serialization",
      deserializedValue,is(value));
}
@Test
origin: alibaba/jstorm

public static String toPrettyJsonString(Object obj) {
  Gson gson2 = new GsonBuilder().setPrettyPrinting().create();
  return gson2.toJson(obj);
}
origin: stackoverflow.com

Editor prefsEditor = mPrefs.edit();
  Gson gson = new Gson();
  String json = gson.toJson(MyObject);
  prefsEditor.putString("MyObject", json);
  prefsEditor.commit();
com.google.gsonGsontoJson

Javadoc

Converts a tree of JsonElements into its equivalent JSON representation.

Popular methods of Gson

  • fromJson
    This method deserializes the specified Json into an object of the specified type. This method is use
  • <init>
  • toJsonTree
    This method serializes the specified object, including those of generic types, into its equivalent r
  • getAdapter
    Returns the type adapter for type.
  • getDelegateAdapter
    This method is used to get an alternate type adapter for the specified type. This is used to access
  • newJsonWriter
    Returns a new JSON writer configured for this GSON and with the non-execute prefix if that is config
  • newJsonReader
    Returns a new JSON reader configured for the settings on this Gson instance.
  • assertFullConsumption
  • doubleAdapter
  • floatAdapter
  • longAdapter
  • fieldNamingStrategy
  • longAdapter,
  • fieldNamingStrategy,
  • toString,
  • atomicLongAdapter,
  • atomicLongArrayAdapter,
  • checkValidFloatingPoint,
  • excluder,
  • newBuilder,
  • fromGson

Popular in Java

  • Creating JSON documents from java classes using gson
  • scheduleAtFixedRate (Timer)
  • onCreateOptionsMenu (Activity)
  • onRequestPermissionsResult (Fragment)
  • DecimalFormat (java.text)
    A concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features desig
  • NumberFormat (java.text)
    The abstract base class for all number formats. This class provides the interface for formatting and
  • Dictionary (java.util)
    Note: Do not use this class since it is obsolete. Please use the Map interface for new implementatio
  • HashSet (java.util)
    HashSet is an implementation of a Set. All optional operations (adding and removing) are supported.
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • Notification (javax.management)
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now