Commit Graph

216 Commits

Author SHA1 Message Date
Marcono1234 4f948dd482
Automatically replace version references on `release:prepare` (#2212)
* Automatically replace version references on `release:prepare`

* Specify encoding and improve placeholder replacements

* Add `since $next-version$` for `JsonArray.asList` and `JsonObject.asMap`
2022-10-24 17:26:43 -07:00
Marcono1234 8451c1fa63
Fix TypeAdapterRuntimeTypeWrapper not detecting reflective TreeTypeAdapter and FutureTypeAdapter (#1787)
* Fix TypeAdapterRuntimeTypeWrapper not detecting reflective TreeTypeAdapter

Previously on serialization TypeAdapterRuntimeTypeWrapper preferred a
TreeTypeAdapter without `serializer` which falls back to the reflective
adapter. This behavior was incorrect because it caused the reflective
adapter for a Base class to be used for serialization (indirectly as
TreeTypeAdapter delegate) instead of using the reflective adapter for
a Subclass extending Base.

* Address review feedback

* Convert TypeAdapterRuntimeTypeWrapperTest to JUnit 4 test

* Prefer wrapped reflective adapter for serialization of subclass

* Detect reflective adapter used as delegate for Gson.FutureTypeAdapter

* Tiny style tweak.

Co-authored-by: Éamonn McManus <emcmanus@google.com>
2022-10-10 16:10:48 -07:00
Marcono1234 3e3266cf48
Perform numeric conversion for primitive numeric type adapters (#2158)
* Perform numeric conversion for primitive numeric type adapters

This should probably not be visible to the user unless they use the
non-typesafe `Gson.toJson(Object, Type)` where unrelated number types can
be used, or when malformed generic containers are used. For example a
`List<Byte>` containing a Float.

This change also has the advantage of avoiding `JsonWriter.value(Number)`
for primitive type adapters. That method has some overhead because it needs
to make sure that the value is a valid JSON number. However, for primitive
numbers this check is redundant.

* Don't call `JsonWriter.value(float)` for backward compatibility

* Fix typo in comments
2022-10-04 07:22:26 -07:00
Marcono1234 28609089fa
Add Javadoc since tags for previously added elements (#2211) 2022-10-01 15:58:26 -07:00
Marcono1234 6b9db2e449
Add Gson.fromJson(..., TypeToken) overloads (#1700)
* Add Gson.fromJson(..., TypeToken) overloads

Previously only Gson.fromJson(..., Type) existed which is however not
type-safe since the generic type parameter T used for the return type is
not bound.
Since these methods are often used in the form
  gson.fromJson(..., new TypeToken<...>(){}.getType())
this commit now adds overloads which accept a TypeToken and are therefore
more type-safe.

Additional changes:
- Fixed some grammar mistakes
- Added javadoc @see tags
- Consistently write "JSON" in uppercase
- More precise placement of @SuppressWarnings("unchecked")

* Add to Gson.fromJson javadoc that JSON is fully consumed

The newly added documentation deliberately does not state which exception
is thrown because Gson.assertFullConsumption could throw either a
JsonIOException or a JsonSyntaxException.

* Remove unnecessary wrapping and unwrapping as TypeToken in Gson.fromJson

Since the actual implementation of Gson.fromJson is TypeToken based, the
TypeToken variant overloads are now the "main" implementation and the other
overloads delegate to them.
Previously the Type variant overloads were the "main" implementation which
caused `TypeToken.getType()` followed by `TypeToken.get(...)` when the
TypeToken variant overloads were used.

* Trim source code whitespaces

* Fix Gson.fromJson(JsonReader, Class) not casting read Object

To be consistent with the other Gson.fromJson(..., Class) overloads the
method should cast the result.

* Replace User Guide link in Gson documentation

* Remove more references to fromJson(..., Type)

* Extend documentation for fromJson(JsonReader, ...)

* Replace some TypeToken.getType() usages

* Address feedback; improve documentation

* Remove fromJson(JsonReader, Class) again

As noticed during review adding this method is source incompatible.
2022-09-19 06:47:11 -07:00
Marcono1234 f7a164d98b
Fail Maven build on compiler warnings; remove some warning suppressions (#2183)
* Fail Maven build on compiler warnings; remove some warning suppressions

* Fix compiler warnings causing failure for newer JDK

* Improve placement of "raw" and "unchecked" warning suppressions

* Adjust javac documentation link

* Fix compilation error on newer JDKs
2022-08-26 17:36:18 -07:00
Marcono1234 7f77ad4ff6
Replace `$Gson$Preconditions.checkNotNull` with `Objects.requireNonNull` (#2180)
* Replace $Gson$Preconditions.checkNotNull with Objects.requireNonNull

* Add back checkNotNull
2022-08-22 07:22:32 -07:00
Marcono1234 4552db2630
Prefer existing adapter for concurrent `Gson.getAdapter` calls (#2153)
Additionally fail fast for null as type (previous null support was broken
and would have thrown NullPointerException further below anyways).
2022-08-01 10:59:04 -07:00
Marcono1234 cbc0af867b
Improve lenient mode documentation (#2122) 2022-06-28 09:48:05 -07:00
Marcono1234 4dda4ec5ba
Use diamond operator when creating generic instances (#2104) 2022-04-17 15:27:21 -07:00
Marcono1234 e82637c485
Add support for reflection access filter (#1905)
* Add support for reflection access filter

* Improve documentation

* Fix compilation errors

* Relax handling for BLOCK_ALL when invoking default constructor

* Improve handling for inherited fields

* Fix accessible test failing for static fields

* Simplify ReflectiveTypeAdapterFactory field writing

* Fix GsonBuilder changes affecting created Gson instances

* Improve documentation

* Improve handling for IllegalAccessException

For Java < 9, AccessibleObject.canAccess is not available and therefore checks
might pass even if object is not accessible, causing IllegalAccessException
later.

* Fix incorrect GsonBuilder.addReflectionAccessFilter documentation
2022-04-17 09:05:18 -07:00
Marcono1234 e2e851c9bc
Add LazilyParsedNumber default adapter (#2060)
* Add LazilyParsedNumber default adapter

* Validate JsonWriter.value(Number) argument

* Fix incorrect JSON number pattern, extend tests
2022-01-28 11:26:28 -08:00
Marcono1234 73216b2ad7
Add more `Gson` default constants to be used by `GsonBuilder` (#2051) 2022-01-12 07:07:55 -08:00
Marcono1234 615c8835d3
Add `GsonBuilder.disableJdkUnsafe()` (#1904)
* Add GsonBuilder.disableJdkUnsafe()

* Address review feedback
2021-12-30 15:08:18 -08:00
Marcono1234 6ffcdf3029
Fix Javadoc warnings and errors (#2040) 2021-12-26 15:30:21 -08:00
Marcono1234 deaa3a6cd9
Fix `Gson.newJsonWriter` ignoring lenient and HTML-safe setting (#1989)
* Improve Gson newJsonWriter and newJsonReader documentation

* Consider lenient and HTML-safe setting for Gson.newJsonWriter

* Remove empty line between imports
2021-11-01 15:11:10 -07:00
Marcono1234 c54caf308c
Deprecate `Gson.excluder()` exposing internal `Excluder` class (#1986) 2021-10-25 11:28:16 -07:00
Lyubomyr Shaydariv fe30b85224
Support arbitrary Number implementation for Object and Number deserialization (#1290)
* Object and Number type adapters number deserialization can be configured

* Change wording of ToNumberStrategy documentation

* Use inline links in doc sparingly

If the element has already been linked before, don't create a link for
every subsequent occurrence.

See also (slightly dated)
https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html#inlinelinks

* Link to default to-number policies in ToNumberStrategy doc

* Reduce code duplication for deserializing Number

* Hide default factory constants of NumberTypeAdapter and ObjectTypeAdapter

This encapsulates the logic a little bit better.
Additionally refactored factory created by NumberTypeAdapter to only create
TypeAdapter once and then have factory reuse that adapter for better
performance.

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
2021-10-08 17:09:43 -07:00
Marcono1234 380c4ec12c Make dependency on java.sql optional 2020-05-23 23:30:53 +02:00
Manuel Kollus 5370b076e3 Removing 'final' accessor from constructor (#1501) 2019-03-28 09:22:36 -07:00
Lorenz Nickel b75e1bbc79 Code cleanup (Removed spaces) (#1474)
* Removed double spaces in comments

* Unified comments

* Removed space

* Removed spaces in code
2019-03-03 11:18:06 -08:00
Márton Braun 4d942db168 Update Gson.java documentation to explicitly state behavior for empty strings (#1464) 2019-02-17 00:32:43 -08:00
Piet van Dongen 5bbc768fa6 Fix JPMS module setup (fixes #1315) (#1402)
* Fix JPMS module setup (fixes #1315)

* Re-added cause to AssertionErrors
2018-10-18 08:49:08 -07:00
inder123 d84e26d80c
Issue 1242: Printing Gson version when throwing AssertionError and IllegalArgumentException (#1321)
On some versions of Android (probably on some variants of the popular Samsung S4 phone), an older version of Gson is suspected to be bundled in, and gets picked up from the system classpath.
For those versions, the applications that include the latest Gson fail unexpectedly. This debug print will help confirm this issue.
2018-05-17 09:41:21 -07:00
Warren Smith 08bbb226f1 Add newBuilder() API (#1142)
* Add Gson.newBuilder API.

* Remove redundant test.

* Address Codacy comments.

* Reduce visibility of GsonBuilder constructor.
2017-09-20 18:53:10 -07:00
Nels Beckman ddcd6aea7d Update fromJson Javadoc. (#1151)
For the string-based fromJson() method, mention that the result will be null if the given string is empty.
2017-09-18 00:08:22 -07:00
Mohammad Yasir 92b52d25d8 Performance fix to avoid reflection-based initialization of null key surrogate 2017-04-12 16:11:20 -04:00
Yutaro Iino 9e5e4ac630 Add missing comma in toString() 2016-12-21 00:10:42 +09:00
Inderjeet Singh 1f859ec769 addressed code review comments. 2016-06-14 16:34:34 -07:00
jwilson 2df65502ed Don't use ThreadLocals for @JsonAdapter factories and getDelegateAdapter(). 2016-06-02 00:33:09 -04:00
Inderjeet Singh 45511fdd15 Added support for JsonSerializer/JsonDeserializer for JsonAdapter annotation.
JsonAdapter is cached per the type of the JsonAdapter class.
Added a test to ensure JsonAdapter works on fields of parameterized types
Keep track of registered JsonAdapters and JsonAdapterFactorys in ThreadLocal.
2016-06-02 00:08:25 -04:00
Baschdl c414b368e1 Corrected documentation, copy&paste error 2016-05-12 14:47:03 +02:00
Ryan Harter a851569ab9 Adds getters for config fields.
This adds simple getters for certain config fields that would be helpful in custom
TypeAdapters to deal with situations like this:
https://github.com/rharter/auto-value-gson/issues/18
2016-04-26 15:30:01 -04:00
gavlyukovskiy 966de9e60d fixed throwing RuntimeException instead of JsonIOException 2016-04-15 21:00:59 +03:00
Scott Brown 9c4f352320 minor javadoc fix ... @code block not treated as html 2016-04-08 19:34:05 -06:00
Inderjeet Singh 34d7521d95 moved the JsonSerializationContext/JsonDeserializationContext fields to where they are used.
Also moved TreeTypeAdapter to internal.bind package for potential use in JsonAdapterAnnotationFactory.
2016-03-28 14:46:02 -07:00
qwwdfsad 31dcfa3ad6 More appropriate usage of null key surrogate in Gson#getAdapter for backward compatibility 2016-03-05 02:08:26 +03:00
qwwdfsad c5611847a3 Gson synchronized map replaced with concurrent hash map 2016-03-05 00:28:25 +03:00
Dongjoon Hyun aa209fa255 Fix some typos in gson comments. 2016-02-15 14:11:23 -08:00
Jake Wharton 3360c93a76 Add setting for leniency on Gson instance.
Add a JsonReader factory (for parity with the JsonWriter one) which provides a configured instance using the Gson settings.
2016-01-18 12:08:11 -05:00
Jake Wharton 7a1c94f986 Remove synthetic accessors from being generated. 2015-12-27 01:39:19 -05:00
Inderjeet Singh 47cc34548d added currency class 2015-11-06 15:41:15 -08:00
Inderjeet Singh 11b26b5256 Added support for AtomicLongArray.
Also added tests to ensure LongSerializationPolicy is honored.
2015-11-05 14:15:46 -08:00
Inderjeet Singh caef762530 added factory fields for consistency 2015-11-05 14:03:51 -08:00
Inderjeet Singh cc54e4dbdd made methods static 2015-11-05 10:46:54 -08:00
Inderjeet Singh 7821b73202 Added support for AtomicInteger, AtomicBoolean, AtomicLong and AtomicIntegerArray. 2015-11-05 10:45:23 -08:00
Inderjeet Singh e5b3f6368d updated minimum JDK version to 1.6.
Added Overrides for methods implementing an interface.
2015-11-04 18:52:20 -08:00
Jake Wharton e81f3eba27 Expose newJsonWriter wrapper instead of just options.
This is more future proof to ensure that consumers holding on to TypeAdapters for efficiency can always encode JSON in the same way as if the Gson instance was used directly.
2015-09-26 19:19:38 -04:00
Jake Wharton 31f80d8659 Merge pull request #700 from google/jwilson_0907_serializenulls_accessor
Expose serializeNulls with an accessor.
2015-09-07 22:44:05 -04:00
jwilson ff2c8f8e8b Expose serializeNulls with an accessor.
Useful for frameworks that build on Gson.
2015-09-07 22:31:43 -04:00