2008-09-01 05:13:32 +02:00
|
|
|
/*
|
|
|
|
* 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.FieldNamingPolicy;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
2008-11-27 01:50:50 +01:00
|
|
|
import com.google.gson.annotations.SerializedName;
|
2008-09-01 05:13:32 +02:00
|
|
|
import com.google.gson.common.TestTypes.ClassWithSerializedNameFields;
|
|
|
|
import com.google.gson.common.TestTypes.StringWrapper;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functional tests for naming policies.
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public class NamingPolicyTest extends TestCase {
|
|
|
|
|
|
|
|
private GsonBuilder builder;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
builder = new GsonBuilder();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGsonWithNonDefaultFieldNamingPolicySerialization() {
|
|
|
|
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
|
|
|
StringWrapper target = new StringWrapper("blah");
|
|
|
|
assertEquals("{\"SomeConstantStringInstanceField\":\""
|
|
|
|
+ target.someConstantStringInstanceField + "\"}", gson.toJson(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGsonWithNonDefaultFieldNamingPolicyDeserialiation() {
|
|
|
|
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
|
|
|
StringWrapper target = new StringWrapper("SomeValue");
|
|
|
|
String jsonRepresentation = gson.toJson(target);
|
|
|
|
StringWrapper deserializedObject = gson.fromJson(jsonRepresentation, StringWrapper.class);
|
|
|
|
assertEquals(target.someConstantStringInstanceField,
|
|
|
|
deserializedObject.someConstantStringInstanceField);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGsonWithSerializedNameFieldNamingPolicySerialization() {
|
|
|
|
Gson gson = builder.create();
|
|
|
|
ClassWithSerializedNameFields expected = new ClassWithSerializedNameFields(5);
|
|
|
|
String actual = gson.toJson(expected);
|
|
|
|
assertEquals(expected.getExpectedJson(), actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGsonWithSerializedNameFieldNamingPolicyDeserialization() {
|
|
|
|
Gson gson = builder.create();
|
|
|
|
ClassWithSerializedNameFields expected = new ClassWithSerializedNameFields(5);
|
|
|
|
ClassWithSerializedNameFields actual =
|
|
|
|
gson.fromJson(expected.getExpectedJson(), ClassWithSerializedNameFields.class);
|
|
|
|
assertEquals(expected.f, actual.f);
|
|
|
|
}
|
2008-11-27 01:50:50 +01:00
|
|
|
|
|
|
|
public void testGsonDuplicateNameUsingSerializedNameFieldNamingPolicySerialization() {
|
|
|
|
Gson gson = builder.create();
|
2008-11-30 01:47:07 +01:00
|
|
|
ClassWithDuplicateFields target = new ClassWithDuplicateFields(10);
|
2008-11-27 01:50:50 +01:00
|
|
|
String actual = gson.toJson(target);
|
|
|
|
assertEquals("{\"a\":10}", actual);
|
|
|
|
|
2008-11-30 01:47:07 +01:00
|
|
|
target = new ClassWithDuplicateFields(3.0D);
|
2008-11-27 01:50:50 +01:00
|
|
|
actual = gson.toJson(target);
|
|
|
|
assertEquals("{\"a\":3.0}", actual);
|
|
|
|
}
|
|
|
|
|
2009-08-31 19:51:47 +02:00
|
|
|
@SuppressWarnings("unused")
|
2008-11-27 01:50:50 +01:00
|
|
|
private static class ClassWithDuplicateFields {
|
2008-11-30 01:47:07 +01:00
|
|
|
public Integer a;
|
|
|
|
@SerializedName("a") public Double b;
|
|
|
|
|
|
|
|
public ClassWithDuplicateFields(Integer a) {
|
|
|
|
this(a, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ClassWithDuplicateFields(Double b) {
|
|
|
|
this(null, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ClassWithDuplicateFields(Integer a, Double b) {
|
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
}
|
2008-11-27 01:50:50 +01:00
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|