Added support in Gson for serializing/deserializing from primitive char.
Created a new class for character/char tests, PrimitiveCharacterTest. This is the start of breaking down PrimitiveTest into smaller chunks. When MiniGson can not find a type adapter for a type, revised the message to say can't handle instead of can't serialize.
This commit is contained in:
parent
fd502f4e79
commit
882c14a367
@ -75,6 +75,7 @@ final class DefaultTypeAdapters {
|
|||||||
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
|
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
|
||||||
|
|
||||||
// Add primitive serializers
|
// Add primitive serializers
|
||||||
|
map.register(char.class, CHARACTER_TYPE_ADAPTER, true);
|
||||||
map.register(Character.class, CHARACTER_TYPE_ADAPTER, true);
|
map.register(Character.class, CHARACTER_TYPE_ADAPTER, true);
|
||||||
map.register(Number.class, NUMBER_TYPE_ADAPTER, true);
|
map.register(Number.class, NUMBER_TYPE_ADAPTER, true);
|
||||||
|
|
||||||
@ -93,6 +94,7 @@ final class DefaultTypeAdapters {
|
|||||||
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
|
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
|
||||||
|
|
||||||
// Add primitive deserializers
|
// Add primitive deserializers
|
||||||
|
map.register(char.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER), true);
|
||||||
map.register(Character.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER), true);
|
map.register(Character.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER), true);
|
||||||
map.register(Number.class, NUMBER_TYPE_ADAPTER, true);
|
map.register(Number.class, NUMBER_TYPE_ADAPTER, true);
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ public final class MiniGson {
|
|||||||
return candidate;
|
return candidate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("This MiniGSON cannot serialize " + type);
|
throw new IllegalArgumentException("This MiniGSON cannot handle " + type);
|
||||||
} finally {
|
} finally {
|
||||||
threadCalls.remove(type);
|
threadCalls.remove(type);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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 junit.framework.TestCase;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Functional tests for Java Character values.
|
||||||
|
*
|
||||||
|
* @author Inderjeet Singh
|
||||||
|
* @author Joel Leitch
|
||||||
|
*/
|
||||||
|
public class PrimitiveCharacterTest extends TestCase {
|
||||||
|
private Gson gson;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
gson = new Gson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPrimitiveCharacterAutoboxedSerialization() {
|
||||||
|
assertEquals("\"A\"", gson.toJson('A'));
|
||||||
|
assertEquals("\"A\"", gson.toJson('A', char.class));
|
||||||
|
assertEquals("\"A\"", gson.toJson('A', Character.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPrimitiveCharacterAutoboxedDeserialization() {
|
||||||
|
char expected = 'a';
|
||||||
|
char actual = gson.fromJson("a", char.class);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
|
||||||
|
actual = gson.fromJson("\"a\"", char.class);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
|
||||||
|
actual = gson.fromJson("a", Character.class);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user