Example #1
0
  public static void main(String[] args) {
    System.out.println("Hello GSON!!!!!");

    MusicArchive archive = new MusicArchive();
    archive.setTitle("Hello Music");
    archive.setMessage("Message");
    archive.setPage(43);

    MusicArchive.DataSetAlbum album = new MusicArchive.DataSetAlbum();
    album.albumId = 12;
    album.albumTitle = "Rubby";
    album.albumProducer = null;
    album.albumProducer = "Rey";

    List<MusicArchive.DataSetAlbum> list = new ArrayList<>();
    list.add(album);
    archive.setDataset(list);

    GsonBuilder builder = new GsonBuilder();
    Gson gson =
        builder
            .setFieldNamingStrategy(
                new FieldNamingStrategy() {
                  @Override
                  public String translateName(Field field) {
                    if (field.getName().equals("albumId")) return "album_id";
                    else return field.getName();
                  }
                })
            .setPrettyPrinting()
            .serializeNulls()
            .create();

    System.out.println(gson.toJson(archive));

    MusicArchive musicArchive = gson.fromJson(inputData, MusicArchive.class);
    System.out.println(musicArchive.getClass());

    MusicArchive music = new MusicArchive();
    music.setPage(1);
    music.setMessage("dfdsfsdfsdfsds");
    music.setTitle("Title");
    music.setLimit(34333);

    Gson gson1 =
        new GsonBuilder()
            .serializeNulls()
            .setPrettyPrinting()
            //                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            //                .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
            //                .setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
            //                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
            .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES)
            .create();

    try (PrintWriter writer = new PrintWriter(System.out)) {
      writer.println("-------------------------New show-----------------------------------");

      writer.println(gson1.toJson(music));
    }

    //
  }
Example #2
0
  public GsonBuilderManager() {
    m_gsonBuilder = new GsonBuilder();

    m_gsonBuilder.setFieldNamingStrategy(new NonPrexFieldNamingStrategy());
  }