gson-comments/gson/src/test/java/com/google/gson/PrimitiveTypeAdapter.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
2.6 KiB
Java
Raw Normal View History

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;
Down to 22 failing tests. Consolidated all of the different code paths that we use to construct instances. We now have an ObjectConstructor class that knows what type it constructs; this means that we don't need to ever do reflection to lookup a constructor at construction time. Cleaned up some buggy type adapters, particularly around handling of null. Removed dead code for object graph navigation. Moved some classes into 'internal' so they are visible to the 'bind' subpackage. Turned some TypeAdapterFactory/TypeAdapter pairs inside out so that the TypeAdapter is now the inner class. This is necessary so that the factories can take parameters. Added an API to request the 'next' type adapter for a type. This allows type adapters to compose other type adapters. We're using this in two places: - where the user has excluded a type from serialization but not deserialization, we need to use the "default" deserialization but interpose null on serialization. We create a type adapter that delegates for one and returns null for the other. - similarly when a DOM type serializer is registered but no deserializer, or vice versa. This is the biggest change to the MiniGson core. For backwards compatibility, return null for the empty string. Simplify JsonSerializationContext/JsonDeserializationContext to simply call through to GSON. SerializeDefault is currently unsupported. More useful error messages when calling getAsBoolean on a JsonNull. Remove currently unused MemoryRefStack. We might need this back again, though wiring it back in will be much more difficult because we don't interject ourselves between the users' various type adapters.
2011-09-11 09:04:56 +02:00
import com.google.gson.internal.Primitives;
2008-09-01 05:13:32 +02:00
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Handles type conversion from some object to some primitive (or primitive wrapper instance).
*
Perform minor code clean-up (#2544) * Perform minor code clean-up Notable changes: - Replace most usages of `<code>` with `{@code ...}` in Javadoc - Add proper summary sentence to `GsonBuilder.enableComplexMapKeySerialization` - Extend documentation for `GsonBuilder.setDateFormat` methods - Fix `TypeToken.isAssignableFrom(Type)` throwing AssertionError in some cases Maybe the method should not throw an exception in the first place, but it might not matter much since it is deprecated already anyway. - Remove outdated `throws NumberFormatException` from internal `JsonReader.nextQuotedValue`; the behavior had been changed by 85ebaf7c3553a5d5c058fd6067824a7c5258d07f - Fix incorrect documentation on JsonScope fields - Fix unit tests having 'expected' and 'actual' switched - Use dedicated Truth methods instead of `isTrue()` / `isFalse()` - Use common helper methods in JsonWriterTest to avoid duplication * Implement `toString()` for ReflectionAccessFilter constants * Address most of the review comments * Add comment about `source.scm.tag=HEAD` warning Actually it looks like the warning is not actually caused by usage of `HEAD` as value, but rather because the project has a snapshot version during development (which is expected though), see https://github.com/apache/maven-artifact-plugin/blob/maven-artifact-plugin-3.5.0/src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java#L140 But this is not a problem either since during release a non-snapshot version is used.
2023-11-19 18:01:19 +01:00
* <p>Used by {@link ParameterizedTypeFixtures.MyParameterizedTypeAdapter}.
*
2008-09-01 05:13:32 +02:00
* @author Joel Leitch
*/
final class PrimitiveTypeAdapter {
2008-09-01 05:13:32 +02:00
2008-11-15 06:13:05 +01:00
@SuppressWarnings("unchecked")
2008-09-01 05:13:32 +02:00
public <T> T adaptType(Object from, Class<T> to) {
Class<?> aClass = Primitives.wrap(to);
if (Primitives.isWrapperType(aClass)) {
2008-09-01 05:13:32 +02:00
if (aClass == Character.class) {
String value = from.toString();
if (value.length() == 1) {
return (T) (Character) from.toString().charAt(0);
}
throw new JsonParseException("The value: " + value + " contains more than a character.");
2008-09-01 05:13:32 +02:00
}
try {
Constructor<?> constructor = aClass.getConstructor(String.class);
return (T) constructor.newInstance(from.toString());
} catch (NoSuchMethodException e) {
throw new JsonParseException(e);
} catch (IllegalAccessException e) {
throw new JsonParseException(e);
} catch (InvocationTargetException e) {
throw new JsonParseException(e);
} catch (InstantiationException e) {
throw new JsonParseException(e);
}
} else if (Enum.class.isAssignableFrom(to)) {
// Case where the type being adapted to is an Enum
// We will try to convert from.toString() to the enum
try {
Method valuesMethod = to.getMethod("valueOf", String.class);
return (T) valuesMethod.invoke(null, from.toString());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
throw new JsonParseException("Can not adapt type " + from.getClass() + " to " + to);
2008-09-01 05:13:32 +02:00
}
}
}