82 lines
2.5 KiB
Java
82 lines
2.5 KiB
Java
![]() |
/*
|
||
|
* Copyright (C) 2008 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 com.google.gson.Gson;
|
||
|
import com.google.gson.GsonBuilder;
|
||
|
|
||
|
import junit.framework.TestCase;
|
||
|
|
||
|
/**
|
||
|
* Performs some functional testing to ensure GSON infrastructure properly serializes/deserializes
|
||
|
* fields that either should or should not be included in the output based on the GSON
|
||
|
* configuration.
|
||
|
*
|
||
|
* @author Joel Leitch
|
||
|
*/
|
||
|
public class FieldExclusionTest extends TestCase {
|
||
|
private static final String VALUE = "blah_1234";
|
||
|
|
||
|
public void testDefaultInnerClassExclusion() throws Exception {
|
||
|
Gson gson = new Gson();
|
||
|
TestInnerClass target = new TestInnerClass(VALUE);
|
||
|
String result = gson.toJson(target);
|
||
|
assertEquals(target.toJson(), result);
|
||
|
|
||
|
gson = new GsonBuilder().create();
|
||
|
target = new TestInnerClass(VALUE);
|
||
|
result = gson.toJson(target);
|
||
|
assertEquals(target.toJson(), result);
|
||
|
}
|
||
|
|
||
|
public void testInnerClassExclusion() throws Exception {
|
||
|
Gson gson = new GsonBuilder().serializeInnerClasses(false).create();
|
||
|
TestInnerClass target = new TestInnerClass(VALUE);
|
||
|
String result = gson.toJson(target);
|
||
|
assertEquals("", result);
|
||
|
}
|
||
|
|
||
|
public void testDefaultNestedStaticClassIncluded() throws Exception {
|
||
|
Gson gson = new Gson();
|
||
|
TestInnerClass target = new TestInnerClass(VALUE);
|
||
|
String result = gson.toJson(target);
|
||
|
assertEquals(target.toJson(), result);
|
||
|
|
||
|
gson = new GsonBuilder().create();
|
||
|
target = new TestInnerClass(VALUE);
|
||
|
result = gson.toJson(target);
|
||
|
assertEquals(target.toJson(), result);
|
||
|
}
|
||
|
|
||
|
private class TestInnerClass extends TestStaticNestedClass {
|
||
|
public TestInnerClass(String value) {
|
||
|
super(value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static class TestStaticNestedClass {
|
||
|
private final String value;
|
||
|
public TestStaticNestedClass(String value) {
|
||
|
this.value = value;
|
||
|
}
|
||
|
|
||
|
public String toJson() {
|
||
|
return "{\"value\":\"" + value + "\"}";
|
||
|
}
|
||
|
}
|
||
|
}
|