A type safe gson adapter.
This library can help mismatch input types keep correct default value, never null. But keep in mind, input JSON should still be legal format.
It doesn't change the Gson original code, just append a TypeAdapterFactory.
Type | Default Value |
---|---|
String | "" |
byte / Byte | (byte)0 |
short / Short | (short)0 |
int / Integer | 0 |
long / Long | 0L |
float / Float | 0f |
double / Double | 0.0 |
boolen / Boolean | false |
char / Character | (char)0 |
Collection | [] |
Map | {} |
Array / GenericArray | [] |
More details: see TypeSafeTest
implementation 'com.dieyidezui.gson:type-safe-adapter:1.0.1'
Gson safeGson = new GsonBuilder()
.registerTypeAdapterFactory(TypeSafeAdapterFactory.newInstance())
.create();
-
Gson.fromJson(String, Type)
will check input String. If null, null returned immediately. -
The minimal supported gson version is
2.2.2
. -
The theory of safe depends Gson's implementation, So it may lead to some imcompatible case if Gson changed specific code in future version.
Copyright (c) 2018-present, dieyidezui.
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.