All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.norberg.automatter.example.SimpleGenericGsonExample Maven / Gradle / Ivy

package io.norberg.automatter.example;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import io.norberg.automatter.gson.AutoMatterTypeAdapterFactory;

import static java.lang.System.out;

public class SimpleGenericGsonExample {

  public static void main(final String... args) throws IOException {
    // Register the AutoMatterTypeAdapterFactory to handle deserialization
    Gson gson = new GsonBuilder()
        .registerTypeAdapterFactory(new AutoMatterTypeAdapterFactory())
        .create();

    GenericFoobar foobar = new GenericFoobarBuilder()
        .foo(17)
        .bar(1, 2, 3)
        .putBaz("hello world", 4711)
        .build();

    String json = gson.toJson(foobar);
    out.println("json: " + json);

    GenericFoobar parsed = gson.fromJson(json, new TypeToken>() {}.getType());
    out.println("parsed: " + parsed);

    out.println("equals: " + foobar.equals(parsed));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy