alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (RawSerializationTest.java)

This example Java source code file (RawSerializationTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

bar, collection, exception, foo, gson, override, rawserializationtest, string, suppresswarnings, testcase, typetoken, util

The RawSerializationTest.java Java example source code

/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.gson.functional;

import java.util.Arrays;
import java.util.Collection;

import junit.framework.TestCase;

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

/**
 * Unit tests to validate serialization of parameterized types without explicit types
 *
 * @author Inderjeet Singh
 */
public class RawSerializationTest extends TestCase {

  private Gson gson;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    gson = new Gson();
  }

  public void testCollectionOfPrimitives() {
    Collection<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
    String json = gson.toJson(ints);
    assertEquals("[1,2,3,4,5]", json);
  }

  public void testCollectionOfObjects() {
    Collection<Foo> foos = Arrays.asList(new Foo(1), new Foo(2));
    String json = gson.toJson(foos);
    assertEquals("[{\"b\":1},{\"b\":2}]", json);
  }

  public void testParameterizedObject() {
    Bar<Foo> bar = new Bar(new Foo(1));
    String expectedJson = "{\"t\":{\"b\":1}}";
    // Ensure that serialization works without specifying the type explicitly
    String json = gson.toJson(bar);
    assertEquals(expectedJson, json);
    // Ensure that serialization also works when the type is specified explicitly
    json = gson.toJson(bar, new TypeToken<Bar(){}.getType());
    assertEquals(expectedJson, json);
  }

  public void testTwoLevelParameterizedObject() {
    Bar<Bar bar = new Bar>(new Bar(new Foo(1)));
    String expectedJson = "{\"t\":{\"t\":{\"b\":1}}}";
    // Ensure that serialization works without specifying the type explicitly
    String json = gson.toJson(bar);
    assertEquals(expectedJson, json);
    // Ensure that serialization also works when the type is specified explicitly
    json = gson.toJson(bar, new TypeToken<Bar>(){}.getType());
    assertEquals(expectedJson, json);
  }

  public void testThreeLevelParameterizedObject() {
    Bar<Bar> bar = new Bar>>(new Bar>(new Bar(new Foo(1))));
    String expectedJson = "{\"t\":{\"t\":{\"t\":{\"b\":1}}}}";
    // Ensure that serialization works without specifying the type explicitly
    String json = gson.toJson(bar);
    assertEquals(expectedJson, json);
    // Ensure that serialization also works when the type is specified explicitly
    json = gson.toJson(bar, new TypeToken<Bar>>(){}.getType());
    assertEquals(expectedJson, json);
  }

  private static class Foo {
    @SuppressWarnings("unused")
    int b;
    Foo(int b) {
      this.b = b;
    }
  }

  private static class Bar<T> {
    @SuppressWarnings("unused")
    T t;
    Bar(T t) {
      this.t = t;
    }
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java RawSerializationTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.