Commit Graph

152 Commits

Author SHA1 Message Date
Marcono1234 541252a9fb Implement DefaultDateTypeAdapter in a type safer way 2020-05-09 17:34:52 +02: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
Warren Smith d9cc7bc60b Make GsonBuilder.create() factory order idempotent. (#1141) 2017-08-14 08:56:16 -07:00
Lyubomyr Shaydariv b8f616c939 Migrate DefaultDateTypeAdapter to streaming adapter (#1070) 2017-05-30 18:12:50 -07:00
guptasourabh 8101ab74e9 list addition optimization (#1038)
* list addition optimization

* Optimized imports

Optimized imports
2017-03-16 21:16:38 -07: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
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
Jesse Wilson dc4e43bb23 Permit users to define type adapters for primitive types and strings.
Also expose an API to get the field naming strategy.
2012-06-30 02:37:49 +00:00
Jesse Wilson 3df2db1f16 Don't permit a type adapter for String to be registered. 2012-04-12 18:27:48 +00:00
Inderjeet Singh 498049b304 updated documentation for registerTypeHierarchyAdapter to cover TypeAdapter. 2011-12-31 06:00:28 +00:00
Inderjeet Singh 46d2d79ba7 Added javadocs for type adapter registration through registerTypeAdapter method. 2011-12-31 05:32:14 +00:00
Jesse Wilson d7fbac0384 Rename TypeAdapter.Factory to TypeAdapterFactory. 2011-12-23 18:27:13 +00:00
Jesse Wilson e2e672740a Fix broken test in registering competing type hierarchy adapters.
I actually tried to replicate this test but got an error "type adapters conflict" when I was doing it. I suspect the problem was that I was trying to use 'Object' as the base of my type hierarchy and that class is somehow special.
2011-12-06 15:29:48 +00:00
Jesse Wilson d5ed0716db Fix type adapter precedence so that last-registered wins (except for tree type hierarchy adapters, which were always last). 2011-12-06 05:09:18 +00:00
Jesse Wilson 8f8e69a364 Add @since tags. 2011-12-02 23:11:28 +00:00
Inderjeet Singh 2da01fb183 Deleted deepCopy as GsonBuilder should not be designed as a reusable object. 2011-11-29 07:58:32 +00:00
Inderjeet Singh ddde79c861 Added a deepCopy() method in GsonBuilder. This allows a web-service to create a pre-configured GsonBuilder and then for each request, create a Gson instance by adding type adapters to a copy.
In TypeAdapter.Factory.create() method, using the term gson to refer to the Gson instance instead of a little ambiguous context.
2011-11-25 05:40:17 +00:00
Jesse Wilson 9a80d095d9 Fix a regression I introduced with the changes to type hierarchy registration. If the registered type was a raw type, we need to also match the parameterizations of that type. 2011-11-23 13:38:25 +00:00
Inderjeet Singh 1c09e24220 inlined typeAdapter and typeHierarchyAdapter methods. Added some documentation for registerTypeHierarchyAdapterFactory. 2011-11-23 09:26:44 +00:00
Jesse Wilson 1794182a56 Commit to factories as the mechanism to lookup type adapters. This uses factories for type hierarchy adapters. We keep a separate list of factories for tree-style adapters registered with registerTypeHierarchyAdapter to guarantee that these come after the non-hierarchy adapters.
This drops support for type hierarchy instance creators. I don't expect this to be a problem. We'll also detect fewer errors where multiple type adapters can serialize the same type. With APIs like getNextTypeAdapter, I think this might actually be an improvement!
2011-11-23 06:16:55 +00:00
Inderjeet Singh d1de4cf676 renamed GsonBuilder.factory() to GsonBuilder.registerTypeAdapterFactory()
revised GsonBuilder.registerTypeAdapter/registerTypeHierarchyAdapter to take streaming type adapters as well. Removed the typeAdapter() and typeHierarchyAdapter() methods from the public API.
2011-11-22 23:56:10 +00:00
Jesse Wilson aa2f61b7d8 Rename GsonExclusionStrategy to Excluder. The new class is its own factory, which simplifies its caller in GsonBuilder. It no longer implements ExclusionStrategy, which allows the callers to pass in a boolean for serialize/deserialize. This allows us to use one excluder for both code paths. The delegate ExclusionStrategy instances might end up not being shared so it has two lists internally. 2011-11-22 07:37:13 +00:00
Jesse Wilson fed332906d Create a single, monolithic class to manage all exclusion strategies. This gets our file size within target of 177KiB.
I intend to follow this up with a builder for our new class to avoid multiple-argument constructor calls.
2011-11-22 06:07:18 +00:00
Jesse Wilson 4da08b0ec6 Remove two implementation classes that weren't generally useful:
- The cache interface has only one implementation. Drop the interface; we can add it back later if necessary.
 - The DefaultTypeAdapters class contains one member class. Just make that a top-level class.
2011-11-21 06:23:42 +00:00
Jesse Wilson 7def596775 Begin to tighten the ExclusionStrategy code. This replaces named classes with anonymous classes wherever we have a single instance of a type. 2011-11-21 06:14:23 +00:00
Jesse Wilson f89e92aa9f Tighten up some of the fields naming policy code. The main thrust of this change is replacing classes like UpperCaseNamingPolicy with the corresponding method calls. Classes like CompositeFieldNamingPolicy are replaced by sequences of method calls. This also replaces unit tests with functional tests.
One nice benefit of this is a 3%/5.7KiB reduction in the size of gson.jar to 184KiB.
2011-11-21 05:08:23 +00:00
Jesse Wilson f777a192ee Remove dead code and fold contents of single-member helper classes into their clients. 2011-11-20 21:02:26 +00:00
Jesse Wilson d391584d48 Register Gson 1.x tree-style adapters in the TypeAdapter.Factory list rather than in the ParameterizedTypeHandlerMap.
The motivation for this change is to give tree-style adapters precedence order in registration. This fixes the test I committed earlier today, where registration order was not honored.

This renamed ParameterizedTypeHandlerMap to the shorter 'TypeMap'. For type adapters, this is now only used for type hierarchy. We still need non-hierarchy support in TypeMap for instance creators; I'll be looking for workarounds to see if further simplification is possible here.
2011-11-20 19:55:01 +00:00
Jesse Wilson 777e17c723 No more system type adapters. 2011-11-20 18:03:46 +00:00
Jesse Wilson 7e760143fd Smash together MiniGson and Gson. This changes Gson to be the MiniGson rather than delegating to the MiniGson. It means that the MiniGson objects passed into streaming type adapters are now fully-capable 'Gson' objects.
The most notable impact of this change is that it adds several new public APIs:
 - The TypeAdapter abstract class
 - The TypeAdapter.Factory interface
 - Four new methods on GsonBuilder to register streaming TypeAdapters (via Factory, via Class, via TypeToken, and as a type hierarchy)
 - Three new methods on Gson to lookup streaming TypeAdapters (by type, by class, and to get the next type adapter) 

Still outstanding:
 - Write beautiful prose to document the new APIs above
 - Change GsonBuilder's precedence so that both old and new-style type adapters are registered in one lot
2011-11-20 15:23:08 +00:00
Inderjeet Singh d3f927eb42 Pure refactorings:
Replaced DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS and DEFAULT_INSTANCE_CREATORS with a single EMPTY_MAP.
Removed obsoleted TODO from Gson.
made ParameterizedTypeHandlerMap.makeUnmodifiable a builder method that returns this instance.
2011-09-30 17:56:40 +00:00
Jesse Wilson 46e65a77c5 Convert RuntimeTypeAdapter to a TypeAdapterFactory; this avoids the need for serializeDefault() 2011-09-28 19:14:46 +00:00
Inderjeet Singh 2f0fbf6bcc deleted code that didnt really do anything as there are no default old-style type adapters. 2011-09-26 17:45:06 +00:00
Inderjeet Singh c71e61cf48 Converted EnumTypeAdapter to new style.
Got rid of default Hierarchy Serializer/Deserializers from DefaultTypeAdapters.
Got rid of methods for gettting default serializers/deserializers/instance creators. Instead we reuse the static final instances.
Fixed warnings in TypeAdapters where a parameterized type T was hiding the parameterized T in methods.
Removed support to unwrap single element array of enums into enum values. Also removed the test that verifies this behavior.
2011-09-16 05:40:05 +00:00
Jesse Wilson a98d6eae47 Fix the map type adapter to support array serialization natively. 2011-09-12 05:51:17 +00:00
Jesse Wilson 25c6ae177b 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 07:04:56 +00:00
Jesse Wilson e756608568 Forbid custom serializers for primitive types (so we can avoid boxing in the reflective and array adapters) 2011-09-09 06:26:21 +00:00
Jesse Wilson aa067056c3 Restore support for long serialization policy. 2011-08-04 22:57:36 +00:00
Inderjeet Singh 9196b23251 Refactoring: Separated type handler maps into two types: one for system-specified handlers and one for user-specified. system-specified handlers are the ones that Gson comes with (DefaultTypeAdapters). 2011-06-24 21:52:59 +00:00
Joel Leitch ecf137fec3 More fixes to JavaDoc. 2011-04-11 19:01:07 +00:00
Joel Leitch c5c7c5bb64 More fixes to JavaDoc. 2011-04-11 18:52:29 +00:00
Joel Leitch 49e7ee05fc Fix method name by making it singular. 2011-04-11 18:44:19 +00:00
Joel Leitch c266097310 Fix minor JavaDoc issue. 2011-04-11 18:33:46 +00:00
Joel Leitch 457b37f08e Replace var-args with single parameter. 2011-04-11 18:09:59 +00:00
Inderjeet Singh 4bd261ae06 Made FieldNamingStrategy2 and its corresponding builder method package-private. 2011-04-07 17:41:35 +00:00
Inderjeet Singh 7afda06253 Added warning in GsonBuilder regarding setting date format correctly. 2011-04-06 00:35:05 +00:00
Inderjeet Singh c8bd121db2 Renamed $Preconditions to $Gson$Preconditions and $Types to $Gson$Types.
Marked the two private classes in $Types as final.
2011-04-06 00:26:57 +00:00
Joel Leitch dc283e1121 Fixed comments from r774. 2011-04-05 23:36:05 +00:00
Joel Leitch f36c1bc222 Very minor cleanup. 2011-04-04 23:17:43 +00:00
Inderjeet Singh 542a17c3bc Incorporated comments from r710 2011-04-04 22:48:34 +00:00
Jesse Wilson f718784f33 Use the date format for java.sql.Date and java.sql.Timestamp.
Fixes issue 230.
2011-03-29 21:36:19 +00:00
Jesse Wilson 4efb133b4a bling bling
Prefix internal classes with $ to prevent them from interfering with IDE's auto import functionality.
2011-03-29 21:24:26 +00:00
Joel Leitch 861e047a62 Create an "internals" package for classes that we do not want to share, but is needed across multiple pacakges.
As well, move non-Gson specific classes, such as Preconditions, to this new "internals" package.
2011-03-21 22:30:35 +00:00
Joel Leitch 279c0e87ed Update Precondition checks and have it throw a NPE instead. 2011-03-16 07:23:44 +00:00
Joel Leitch 7b223a8983 Adding JavaDoc to the enableComplexMapKeySerialization method on GsonBuilder. 2011-03-16 07:05:24 +00:00
Joel Leitch e57ef0908b Add method to GsonBuilder to enable complex key serialization of maps. 2011-03-15 15:37:41 +00:00
Inderjeet Singh baf9591d3b Made constructors package private for package private classes. 2011-03-10 23:55:16 +00:00
Inderjeet Singh 52288d7127 rawtypes -> unchecked
Fixed spurious eclipse warnings on deprecation, missing classes or parameters, etc.
2011-02-24 22:47:55 +00:00
Joel Leitch 114633fbf9 Provide a means to add serialization or deserialization specific exclusion strategies. 2011-02-04 03:09:41 +00:00
Joel Leitch fb7bd7b1b7 Deprecate the old FieldNamingStrategy interface and open up the FieldNamingStrategy2 instead to take its place. 2011-01-22 22:27:30 +00:00
Joel Leitch b883f8f4aa Add new "Mode" enum and carry mode through Gson so exclusion strategies know whether it is currently serializing or deserializing.
Deprecate old ExclusionStrategy code since this new approach is more powerful.
2011-01-22 22:15:06 +00:00
Inderjeet Singh 2b9f81e8b5 Registering default type hierarchy adapters first and allow users to override them.
This allows the default EnumTypeAdapter to be overridden for a specific hierachy adapter for Enum with anonymized sub-classes.
2011-01-19 23:28:28 +00:00
Inderjeet Singh 6818edecf9 enabled GsonBuilder.registerTypeHierarchyAdapter since head is now for Gson 1.7 2010-11-25 00:16:06 +00:00
Inderjeet Singh db3f19e881 dropping GsonBuilder.registerTypeHierarchyAdapter() from 1.6 release. 2010-11-24 23:13:29 +00:00
Jesse Wilson dea7ab89fe Restore pretty printing. The pretty printing format isn't as compact as the previous format - for example arrays of integers are printed one-per-line, whereas the previous format compacted these to all sit on the same line. 2010-11-01 23:03:41 +00:00
Inderjeet Singh 2b993d83b6 Made the GsonBuilder registerTypeHierarchyAdapter a public method.
Updated the Gson version number to 1.6 and added @since tag for the new classes
2010-10-27 23:53:09 +00:00
Inderjeet Singh 2b1f3eec15 Removed a bunch of unused code and unnecessary else statements. 2010-09-28 13:42:43 +00:00
Jesse Wilson 747e3c3051 Support mixed streaming and databinding with new APIs. 2010-09-02 00:15:23 +00:00
Inderjeet Singh 24fd66f645 removed @since for non-public classes 2010-08-20 00:35:48 +00:00
Inderjeet Singh dc60cb1931 Made FieldNamingStrategy2 is package protected class.
Made type hierarchy adapter registration package protected.
Marked some of the classes final.
Minor changes in the performance tests.
2010-08-19 00:10:42 +00:00
Joel Leitch 89c18452ae Ensure date with pattern is not override with the "default" date type adapter when a custom JsonSerializer/JsonDeserializer is registered. 2010-08-18 23:58:52 +00:00
Inderjeet Singh 8aedbc84db Added a new GsonBuilder option to register a type adapter for a type hierarchy instead of a single type.
Upgraded Gson version to 1.5 since a new API call is being added.
2010-06-21 23:26:06 +00:00
Joel Leitch e3af076ff2 Deprecate the FieldNamingStrategy interface and replace it with FieldNamingStrategy2. This is the first step to help make it easy to cache field annotations across all instances of a class, etc. 2010-01-09 22:43:27 +00:00
Joel Leitch 6387c9028e Updated JavaDoc formatting 2009-10-09 15:26:34 +00:00
Joel Leitch 10c39317a1 Makes ExclusionStrategy public. 2009-10-08 21:52:56 +00:00
Joel Leitch 839b0c2f94 Refactored exclusion strategies so that they can easily be exposed as part of the public API. 2009-10-07 09:23:14 +00:00
Inderjeet Singh 8567fe6c10 Implementing code review comment from r428: Using Polymorphism for exclusion strategies for expose annotations. 2009-10-01 01:37:57 +00:00
Inderjeet Singh 3b0f8f4340 Removed all the JDK warnings about unused fields in test classes or unused constructors for use by Gson or instanceof calls on parameterized types. 2009-09-23 17:45:16 +00:00
Joel Leitch 18b301dfeb - Serializing of Object arrays.
- Fixed incorrect warning
2009-09-22 19:04:27 +00:00
Inderjeet Singh 82771f006c removed Java 1.5 warnings 2009-08-31 17:51:47 +00:00
Inderjeet Singh 6d50bcea87 Added serialize and deserialize parameters to the Expose annotation that control whether a field gets exposed during serialization or deserialization. 2009-05-19 23:47:53 +00:00
Inderjeet Singh b2b2e5dc0a Implemented enhancement request from issue 42 by supporting a new GsonBuilder setting generateNonExecutableJson() that prefixes the generated JSON with some special text that makes the output non-executable javascript. Gson now recognizes this special text in the input while parsing the JSON in fromJson and filters it out, if present. 2009-03-17 21:15:10 +00:00
Inderjeet Singh 8f456831d4 Fixed Issue 104 by making FieldNamingStrategy to be public and allowing FieldNamingStrategy to be set in GsonBuilder 2009-03-11 21:53:02 +00:00
Joel Leitch 764e4d9aca Performance fixes after doing some profiling. 2009-01-20 08:38:21 +00:00
Joel Leitch 6a80791f13 Code cleanup and some minor performance fixes. 2009-01-20 01:36:54 +00:00
Joel Leitch e2cfc0cc19 Update GsonBuilder API to accept an enum for the serialize Long as string. This will be useful if/when we implement support to serialize a long type as an array of integers. 2009-01-11 06:11:29 +00:00
Inderjeet Singh 3690d362b9 incorporated feedback from r358 2008-12-30 22:42:36 +00:00
Inderjeet Singh e839336eea Incorporated comments from the code review r355 2008-12-30 19:03:43 +00:00
Inderjeet Singh 05f54f3552 Incorporated code review comments from r350 2008-12-30 18:54:33 +00:00
Joel Leitch 73d93e3322 Provide a setting to allow a client to skip the escaping of special HTML characters.
As well, remove the "/" from the list of special HTML characters since it is causing some incompatibilities.
2008-12-28 23:05:22 +00:00
Joel Leitch 1c87bd5993 Some minor update to our JavaDoc 2008-12-28 06:51:11 +00:00
Joel Leitch f923f7f537 Add "@since" JavaDoc to the new methods in GsonBuilder. 2008-12-28 03:35:07 +00:00
Joel Leitch 458f2baa2f Added special serialization of "Long". Now the client has the ability to output a long field as a JSON "String". This is useful for JavaScript clients that need to handle long values.
As well, this change does a major clean up of the custom type adapter handling and ParameterizedTypeMap creation.
2008-12-28 03:23:36 +00:00
Joel Leitch 859af0025c Implement suggestions from self review of r350. 2008-12-28 02:05:40 +00:00
Joel Leitch 54a480774d Add the ability to configure Gson to exclude serializing and deserializing of all "Inner Classes". 2008-12-28 02:00:31 +00:00
Inderjeet Singh 51881c7f4a Added a GsonBuilder setting to enable serialization of special double types, NaN, Infinity, and -Infinity. 2008-12-20 01:26:14 +00:00
Joel Leitch cf2a457af7 Removed the TypeAdapter object from GSON now that the primitive objects and fields are created by a custom Type Adapter. 2008-11-15 05:11:28 +00:00
Inderjeet Singh bc2c25f235 Removed ExceptionWrapper from serializers.
This is done since it is improper to throw a parse exception from serializers. Moreover, the serializers dont deal with external input, they deal with classes, so they need not guard against spurious input.
2008-11-14 02:17:19 +00:00
Inderjeet Singh 9dfa454f6d Fixed issue 63 where Gson could not operate in a thread-safe manner. Resolved the issue by recreating ObjectNavigatorFactory for each call instead of reusing the same object everywhere. This is needed since ObjectNavigatorFactory had MemoryRefStack that was incorrectly being shared across calls. 2008-10-20 20:09:42 +00:00