Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing/Deserializing simple maps should be better supported #45

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 12 comments
Closed

Comments

@GoogleCodeExporter
Copy link

Attempting to simply serialize a map results in fairly useless output:

// dead simple map
Map<String, Object> m = new ArrayOrderedMap<String, Object>();
m.put("id", 123);
m.put("thing", "AZ");

// serialize (annoyingly requires the typetoken thing)
String encoded = gson.toJson(data, new TypeToken<Map<String, Object>>()
{}.getType());

encoded = {"id":{},"thing":{}}

It should have been:
encoded = {"id":123,"thing":"AZ"}

This should really be able to handle the simple example of a map of
primitive/simple objects much better. The same type of thing happens when
attempting to read back in the data.

With a more realistic example it is even worse because another map placed
inside the first map results in an exception like so:
com.google.gson.JsonParseException: The JsonSerializer
com.google.gson.DefaultTypeAdapters$MapTypeAdapter@b27bb5 failed to
serialized object {name=aaron, date=Mon Sep 15 11:58:33 BST 2008, num=456,
array=[Ljava.lang.String;@fe3238} given the type class java.lang.Object
    at
com.google.gson.JsonSerializerExceptionWrapper.serialize(JsonSerializerException
Wrapper.java:61)
    at
com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(JsonSerializati
onVisitor.java:177)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:144)
    at
com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationConte
xtDefault.java:47)
    at
com.google.gson.DefaultTypeAdapters$MapTypeAdapter.serialize(DefaultTypeAdapters
.java:301)
    at
com.google.gson.DefaultTypeAdapters$MapTypeAdapter.serialize(DefaultTypeAdapters
.java:293)
    at
com.google.gson.JsonSerializerExceptionWrapper.serialize(JsonSerializerException
Wrapper.java:48)
    at
com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(JsonSerializati
onVisitor.java:177)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:144)
    at
com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationConte
xtDefault.java:47)
    at com.google.gson.Gson.toJson(Gson.java:272)
    at com.google.gson.Gson.toJson(Gson.java:228)
    at
org.sakaiproject.entitybroker.impl.EntityEncodingManager.encodeData(EntityEncodi
ngManager.java:586)
    at
org.sakaiproject.entitybroker.impl.EntityEncodingManagerTest.testEncode(EntityEn
codingManagerTest.java:243)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestR
eference.java:130)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:673)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java
:386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.jav
a:196)
Caused by: java.lang.IllegalArgumentException: Map objects need to be
parameterized unless you use a custom serializer. Use the
com.google.gson.reflect.TypeToken to extract the ParameterizedType.
    at com.google.gson.TypeInfoMap.<init>(TypeInfoMap.java:34)
    at
com.google.gson.DefaultTypeAdapters$MapTypeAdapter.serialize(DefaultTypeAdapters
.java:298)
    at
com.google.gson.DefaultTypeAdapters$MapTypeAdapter.serialize(DefaultTypeAdapters
.java:293)
    at
com.google.gson.JsonSerializerExceptionWrapper.serialize(JsonSerializerException
Wrapper.java:48)
    ... 31 more



Original issue reported on code.google.com by azeckoski on 15 Sep 2008 at 11:00

@GoogleCodeExporter
Copy link
Author

Extra info on the deserializing:
json = {"id":123,"thing":"AZ"}

Map<String, Object> decoded = gson.fromJson(data, new TypeToken<Map<String,
Object>>() {}.getType());

produces a map with: {id=java.lang.Object@e6612c, thing=java.lang.Object@d704f0}
(seems to be instances of Object with no data)

Original comment by azeckoski on 15 Sep 2008 at 1:29

@GoogleCodeExporter
Copy link
Author

First off, I'd like to start with some background information.  When you are 
defining
types (or local variables) that have type parameters, the JVM drops the actual 
type
parameters and associates everything as "Object".  This is known as "type 
erasure". 
In order for a Java Program to retrieve the actual type parameters at run-time, 
you
need to leverage the TypeToken object (this methodology was established by 
GUICE ---
see
http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/TypeLiter
al.html).

Originally, we implemented Gson so that it could "serialize" these kinds of 
objects
without requiring the use of TypeToken; however, deserializing it back into the 
real
object requires it since the JSON output has no type information in it.  As 
well,
with this approach, it meant you were serializing the "real" object which meant 
that
some fields on the real object would be added to the JSON output.  Therefore, 
if you
had a List<A> and added both A and B objects (i.e. B extends A) some objects 
would
expose extra fields in the output.  We decided to take the more explicit route 
and
force the client to provide us the type parameters of the top-level object that 
is
being serialized.

The common use of a Map or List is that you populate the List with the same 
object
types.  Passing in Object means that you can add any instance of a class that 
you
desire to the data structure.  I know there are exceptions to this best 
practice, but
we do not want to implement this corner case scenario.  Instead, if you really 
do
want to use a list of Objects, then do as the exception message says and write a
"custom" (de)serializer (you can bind it specifically to a Map<String, Object> 
and
have the default Gson map serializer handle everything else).

As for a Map of Maps (i.e. Map<String, Map<String, Integer>>) this is already
supported and works well as long as you pass in the actual type object (i.e. new
TypeToken<Map<String, Map<String, Integer>>>() {}.getType())

Here's an example:
public static void main(String[] args) {
  Type mapType = new TypeToken<Map<String, Map<String, Integer>>>() {}.getType();
  Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
  Map<String, Integer> value1 = new HashMap<String, Integer>();
  value1.put("lalala", 78);
  value1.put("haha", 9999);
  map.put("id", value1);

  Map<String, Integer> value2 = new HashMap<String, Integer>();
  value2.put("nahhd", 121112);
  value2.put("uuywss", 19987);
  map.put("thing", value2);

  Map<String, Integer> value3 = new HashMap<String, Integer>();
  map.put("other", value3);

  Gson gson = new Gson();
  String json = gson.toJson(map, mapType);
  System.out.println(json);

  Map<String, Map<String, Integer>> deserializedMap = gson.fromJson(json, mapType);
  System.out.println(deserializedMap);
}

=========== OUTPUT ===========
{"thing":456,"id":123}
{"other":{},"thing":{"nahhd":121112,"uuywss":19987},"id":{"lalala":78,"haha":999
9}}
{other={}, thing={nahhd=121112, uuywss=19987}, id={lalala=78, haha=9999}}


For now, I am closing this off as "Working as Designed".  Maybe I am not 
completely
following your issue and if that is the case, please start up a new discussion 
in our
Gson discussion group.

Thanks,
Joel

Original comment by joel.leitch@gmail.com on 16 Sep 2008 at 8:57

  • Changed state: Invalid

@GoogleCodeExporter
Copy link
Author

We solved our problem by using a different library but I wanted to put a 
comment here
anyway.

So what happens if I want to do this?
Map<String, Number>
or
Map<String, Serializable>

(it seems to fail)

It seems that this is designed to only work for the basic case where I have 
really
simple and non-nested structures where all the beans are easily instantiable 
and not
superclasses. It is a shame that this is considered working as designed.

Original comment by azeckoski on 18 Sep 2008 at 9:01

@GoogleCodeExporter
Copy link
Author

I am glad to hear that you found something that works for you, but it's too bad 
you
are unable to use Gson.  I'd still like to follow up on this issue because it is
user's like yourself that will help to advance this library.

First off, are you serializing and deserializing an object of type Map<String,
Number>?  If it is serialization only, than that is a much "easier" problem to 
solve
because we have the runtime types.  As for "deserializing" this kind of object, 
we
have provided our clients with the concept of a custom "Type Adapter".  You 
should be
able to write a type as follows to get it to work with "Number":

  public static class NumberTypeAdapter 
      implements JsonSerializer<Number>, JsonDeserializer<Number>,
InstanceCreator<Number> {

    public JsonElement serialize(Number src, Type typeOfSrc, JsonSerializationContext
context) {
      return new JsonPrimitive(src);
    }

    public Number deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
        throws JsonParseException {
      JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
      if (jsonPrimitive.isNumber()) {
        return jsonPrimitive.getAsNumber();
      } else {
        throw new IllegalStateException("Expected a number field, but was " + json);
      }
    }

    public Number createInstance(Type type) {
      return 1L;
    }
  }

  public static void main(String[] args) {
    Map<String, Number> map = new HashMap<String, Number>();    
    map.put("int", 123);
    map.put("long", 1234567890123456789L);
    map.put("double", 1234.5678D);
    map.put("float", 1.2345F);
    Type mapType = new TypeToken<Map<String, Number>>() {}.getType();

    Gson gson = new GsonBuilder().registerTypeAdapter(Number.class, new
NumberTypeAdapter()).create();
    String json = gson.toJson(map, mapType);
    System.out.println(json);

    Map<String, Number> deserializedMap = gson.fromJson(json, mapType);
    System.out.println(deserializedMap);
  }

========== OUTPUT ==========
{"double":1234.5678,"float":1.2345,"int":123,"long":1234567890123456789}
{double=1234.5678, float=1.2345, int=123, long=1234567890123456789}


We should probably just include the above type adapter as a default in Gson and 
I
will discuss this with Inderjeet.  There is a bug, however, since you actually 
have
to specify a "instance creator" for this type of object (i.e. primitive), but I 
will
have that fixed by the next release.  You should be able to write something 
similar
as above for "Serializable".

I hope this information is helpful and thanks for the all the feedback on this 
library.

Original comment by joel.leitch@gmail.com on 27 Sep 2008 at 8:32

@GoogleCodeExporter
Copy link
Author

I modify the MapTypeAdapter to match the jdk14's Map
======================================================
public JsonElement serialize(Map src, Type typeOfSrc, JsonSerializationContext 
context) {
      JsonObject map = new JsonObject();
      //Type childType = new TypeInfoMap(typeOfSrc).getValueType();
      for (Iterator iterator = src.entrySet().iterator(); iterator.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iterator.next();
        Object obj = entry.getValue();
        JsonElement valueElement = context.serialize(obj, obj.getClass());
        ---------------------------------------------------------------------modified
        map.add(entry.getKey().toString(), valueElement);
      }
      return map;
    }
--------------------------------------------------------------------------------

and then the map class can be used like this:
  HashMap aaaa = new HashMap();
  aaaa.put("aa", 1212);
  aaaa.put("bb", "fasdfa");
  System.out.println(gson.toJson(aaaa));
==========output=================
{"bb":"fasdfa","aa":1212}
=================================

It can run, good or bad? because there are lot's of  jdk14's source code in many
project's.

Original comment by zhaojinz...@gmail.com on 21 Oct 2008 at 9:53

@GoogleCodeExporter
Copy link
Author

Thanks for providing the code snippet. This will not work properly in case of 
genericized maps since in those cases it is important to use the type specified 
in 
the field declaration instead of the actual type. I have made similar fixes for 
Issue 
54 and 58 that I will apply in this case as well. 

Original comment by inder123 on 21 Oct 2008 at 3:32

@GoogleCodeExporter
Copy link
Author

I have fixed this issue in r277 

Now, you should be able to serialize raw maps. The deserialization continues to 
require parameterized type. 

Original comment by inder123 on 21 Oct 2008 at 10:41

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

Thank you for fixing this issue: 
In java land, you really shouldn't be instantiating Map<String, Object> but 
since
we're dealing with JSON world, it actually makes a lot of sense. 
Consider Map<String, Object> map;
map.put("field1", 123);
map.put("field2", "myfield2");

What is gson.toJson(map)???
It's a javascript object o where o.field1 is the number 123 and o.field2 is the
string myfield2!

Original comment by SystemIn...@gmail.com on 22 Jan 2010 at 9:43

@GoogleCodeExporter
Copy link
Author

json convert to map<Integer,MyClass> it have problem !
how to  do ?

Original comment by demog...@gmail.com on 6 Mar 2010 at 6:43

@GoogleCodeExporter
Copy link
Author

If you do json eval() in javascript or python, you get a dictionary. Inside the 
dictionary, it has String/Number or nested dictionaries. eval doesnt expect 
these type declarations.
I would expect the same on static language as well - Maps with default Number 
(lossless datatype like Double) datatype for deserialization.

Original comment by mani.dor...@gtempaccount.com on 25 Jun 2010 at 6:12

@GoogleCodeExporter
Copy link
Author

Hi demograp,

Did you forget to implement a default constructor for MyClass ?
It was my case, and I solved it doing this.

Hope it helps.

Original comment by kstruil...@gmail.com on 6 Aug 2010 at 2:45

@GoogleCodeExporter
Copy link
Author

I faced similar problems. Easiest solution for me was wrapping the desired map 
in a wrapper object and passing that to gson. I guess in the end that just 
boils down to be the same as providing the TypeToken, but it is a much more 
straightforward solution for those who want a quick fix.

Original comment by dska...@gmail.com on 1 Sep 2011 at 12:23

Marcono1234 pushed a commit to Marcono1234/gson that referenced this issue Apr 8, 2023
Marcono1234 pushed a commit to Marcono1234/gson that referenced this issue Apr 8, 2023
Marcono1234 pushed a commit to Marcono1234/gson that referenced this issue Apr 8, 2023
Marcono1234 pushed a commit to Marcono1234/gson that referenced this issue Apr 8, 2023
Marcono1234 pushed a commit to Marcono1234/gson that referenced this issue Apr 8, 2023
eamonnmcmanus pushed a commit that referenced this issue May 30, 2023
* Feat #6: Add strict flag to Gson and GsonBuilder

* Test #2: Add failing tests for capitalized keywords

* Feat #2: JsonReader does not read (partially) capitalized keywords if strict mode is used

* Feat #3: Added implementation and tests for JSONReader not accepting specific escape sequence representing in strict mode

* Test #3: Simplify test cases by removing unnecessary array

* Feat #3: Improve error by including the illegal character

* Feat #5: JsonReader does not allow unespaced control flow characters in strict mode

* Test #5: Test unespaced control flow characters in strict mode

* Feat #4: Disallow espaced newline character in strict mode

* Test #4: Add tests for (dis)allowing newline character depensding on strictness

* Test #5: Test case for unescaped control char in non-strict mode

* Test #2: Simplify test cases

* Feat #13: Change leniency API to Strictness enum in JsonReader, Gson, and GsonBuilder

* Feat #15: Change JsonWriter API to also use Strictness

* Test #15: Test Strictness in JsonWriter API

* Doc #15: Add and update documentation for Strictness in JsonWriter API

* refactor #12: Fixed typos and empty catch brackets in tests

* refactor #12: Resolved importing wildcards, made some lines adhere to Google java style

* #5 Add test case for unescaped control characters

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

---------

Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>

* Doc #17: Add and change javadoc of public methods

* Doc #17: Update JavaDoc in JsonReader and Strictness

* Doc #17: Update JavaDoc in Gson and GsonBuilder

* Test #34: Add tests for setting strictness through GsonBuilder

* Fix: Add Fix broken test

* Fix: Invalid JavaDoc in Gson.java

* Doc #17: update outdated javadoc

* #37: Resolve more PR feedback 

* Fix #37: Resolve various PR comments

* Fix #37: Resolve various PR comments

* Refactor #35: Refactor JsonReader#peekKeyword to reduce the amount of strictness checks (#39)

* Doc #40: Update JavaDoc based on PR feedback

* Doc #40: Update old RFC in GsonBuilder documentation

* Doc #40: Fix formatting error in JavaDoc

* Doc #40: Add tests for setting strictness and lenient to JsonReaderTest

* Test #43: Changed tests to make use of assertThrows

* test #43: Changed tests to make use of assertThrows as per feedback

* Test #43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* Test #43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* test #43: Resolve PR recommendations

* Test #43: Mini change to TC

* Test #43: Mini change to TC

---------

Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>

* doc #46: Resolved comments in main PR

* Feat #45: Change Gson.fromJson and Gson.toJson to be strict when the provided writer/reader is strict

* Fix #45: Small type

* Update gson/src/test/java/com/google/gson/stream/JsonReaderTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fix #45: Resolve various comments by Marcono1234

* Update gson/src/main/java/com/google/gson/GsonBuilder.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fix #45: Resolve various comments by Marcono1234

* Fix #45: Resolve various comments by eamonmcmanus

* Strictness mode follow-up

* Update Troubleshooting.md and Gson default lenient mode documentation

* Always use GSON strictness when set.

* Rename Strictness.DEFAULT to Strictness.LEGACY_STRICT

* Update JavaDoc with new strictness functionality

* Replace default with legacy strict for JsonReader javadoc

* Add JSONReader test cases for U2028 and U2029

* Refactor JSONReader#peekKeyWord() based on @eamonmcmanus's suggestion

* Deprecate setLenient in favor of setStrictness

---------

Co-authored-by: Carl Peterson <unknown>
Co-authored-by: Gustaf Johansson <gustajoh@kth.se>
Co-authored-by: gustajoh <58432871+gustajoh@users.noreply.github.com>
Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
eamonnmcmanus pushed a commit that referenced this issue May 31, 2023
* Feat #6: Add strict flag to Gson and GsonBuilder

* Test #2: Add failing tests for capitalized keywords

* Feat #2: JsonReader does not read (partially) capitalized keywords if strict mode is used

* Feat #3: Added implementation and tests for JSONReader not accepting specific escape sequence representing in strict mode

* Test #3: Simplify test cases by removing unnecessary array

* Feat #3: Improve error by including the illegal character

* Feat #5: JsonReader does not allow unespaced control flow characters in strict mode

* Test #5: Test unespaced control flow characters in strict mode

* Feat #4: Disallow espaced newline character in strict mode

* Test #4: Add tests for (dis)allowing newline character depensding on strictness

* Test #5: Test case for unescaped control char in non-strict mode

* Test #2: Simplify test cases

* Feat #13: Change leniency API to Strictness enum in JsonReader, Gson, and GsonBuilder

* Feat #15: Change JsonWriter API to also use Strictness

* Test #15: Test Strictness in JsonWriter API

* Doc #15: Add and update documentation for Strictness in JsonWriter API

* refactor #12: Fixed typos and empty catch brackets in tests

* refactor #12: Resolved importing wildcards, made some lines adhere to Google java style

* #5 Add test case for unescaped control characters

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

---------

Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>

* Doc #17: Add and change javadoc of public methods

* Doc #17: Update JavaDoc in JsonReader and Strictness

* Doc #17: Update JavaDoc in Gson and GsonBuilder

* Test #34: Add tests for setting strictness through GsonBuilder

* Fix: Add Fix broken test

* Fix: Invalid JavaDoc in Gson.java

* Doc #17: update outdated javadoc

* #37: Resolve more PR feedback

* Fix #37: Resolve various PR comments

* Fix #37: Resolve various PR comments

* Refactor #35: Refactor JsonReader#peekKeyword to reduce the amount of strictness checks (#39)

* Doc #40: Update JavaDoc based on PR feedback

* Doc #40: Update old RFC in GsonBuilder documentation

* Doc #40: Fix formatting error in JavaDoc

* Doc #40: Add tests for setting strictness and lenient to JsonReaderTest

* Test #43: Changed tests to make use of assertThrows

* test #43: Changed tests to make use of assertThrows as per feedback

* Test #43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* Test #43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* test #43: Resolve PR recommendations

* Test #43: Mini change to TC

* Test #43: Mini change to TC

---------

Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>

* doc #46: Resolved comments in main PR

* Feat #45: Change Gson.fromJson and Gson.toJson to be strict when the provided writer/reader is strict

* Fix #45: Small type

* Update gson/src/test/java/com/google/gson/stream/JsonReaderTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fix #45: Resolve various comments by Marcono1234

* Update gson/src/main/java/com/google/gson/GsonBuilder.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fix #45: Resolve various comments by Marcono1234

* Fix #45: Resolve various comments by eamonmcmanus

* Strictness mode follow-up

* Update Troubleshooting.md and Gson default lenient mode documentation

* Always use GSON strictness when set.

* Rename Strictness.DEFAULT to Strictness.LEGACY_STRICT

* Update JavaDoc with new strictness functionality

* Replace default with legacy strict for JsonReader javadoc

* Add JSONReader test cases for U2028 and U2029

* Refactor JSONReader#peekKeyWord() based on @eamonmcmanus's suggestion

* Deprecate setLenient in favor of setStrictness

---------

Co-authored-by: Carl Peterson <unknown>
Co-authored-by: Gustaf Johansson <gustajoh@kth.se>
Co-authored-by: gustajoh <58432871+gustajoh@users.noreply.github.com>
Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
eamonnmcmanus added a commit that referenced this issue Jul 30, 2023
* Strict mode for JSON parsing (#2323)

* Feat #6: Add strict flag to Gson and GsonBuilder

* Test #2: Add failing tests for capitalized keywords

* Feat #2: JsonReader does not read (partially) capitalized keywords if strict mode is used

* Feat #3: Added implementation and tests for JSONReader not accepting specific escape sequence representing in strict mode

* Test #3: Simplify test cases by removing unnecessary array

* Feat #3: Improve error by including the illegal character

* Feat #5: JsonReader does not allow unespaced control flow characters in strict mode

* Test #5: Test unespaced control flow characters in strict mode

* Feat #4: Disallow espaced newline character in strict mode

* Test #4: Add tests for (dis)allowing newline character depensding on strictness

* Test #5: Test case for unescaped control char in non-strict mode

* Test #2: Simplify test cases

* Feat #13: Change leniency API to Strictness enum in JsonReader, Gson, and GsonBuilder

* Feat #15: Change JsonWriter API to also use Strictness

* Test #15: Test Strictness in JsonWriter API

* Doc #15: Add and update documentation for Strictness in JsonWriter API

* refactor #12: Fixed typos and empty catch brackets in tests

* refactor #12: Resolved importing wildcards, made some lines adhere to Google java style

* #5 Add test case for unescaped control characters

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Feat #5: add new lines to make JsonReader able to detect unescaped control characters (U+0000 through U+001F) and throw exceptions.

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

* Test #11: Added two tests for testing implementation of control character handling in strict mode and moved the implementation to nextQuotedValue

---------

Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>

* Doc #17: Add and change javadoc of public methods

* Doc #17: Update JavaDoc in JsonReader and Strictness

* Doc #17: Update JavaDoc in Gson and GsonBuilder

* Test #34: Add tests for setting strictness through GsonBuilder

* Fix: Add Fix broken test

* Fix: Invalid JavaDoc in Gson.java

* Doc #17: update outdated javadoc

* #37: Resolve more PR feedback 

* Fix #37: Resolve various PR comments

* Fix #37: Resolve various PR comments

* Refactor #35: Refactor JsonReader#peekKeyword to reduce the amount of strictness checks (#39)

* Doc #40: Update JavaDoc based on PR feedback

* Doc #40: Update old RFC in GsonBuilder documentation

* Doc #40: Fix formatting error in JavaDoc

* Doc #40: Add tests for setting strictness and lenient to JsonReaderTest

* Test #43: Changed tests to make use of assertThrows

* test #43: Changed tests to make use of assertThrows as per feedback

* Test #43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* Test #43: Update JsonWriterTest#testStrictnessNull to use assertThrows

* test #43: Resolve PR recommendations

* Test #43: Mini change to TC

* Test #43: Mini change to TC

---------

Co-authored-by: Marten Voorberg <martenvoorberg@gmail.com>

* doc #46: Resolved comments in main PR

* Feat #45: Change Gson.fromJson and Gson.toJson to be strict when the provided writer/reader is strict

* Fix #45: Small type

* Update gson/src/test/java/com/google/gson/stream/JsonReaderTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fix #45: Resolve various comments by Marcono1234

* Update gson/src/main/java/com/google/gson/GsonBuilder.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fix #45: Resolve various comments by Marcono1234

* Fix #45: Resolve various comments by eamonmcmanus

* Strictness mode follow-up

* Update Troubleshooting.md and Gson default lenient mode documentation

* Always use GSON strictness when set.

* Rename Strictness.DEFAULT to Strictness.LEGACY_STRICT

* Update JavaDoc with new strictness functionality

* Replace default with legacy strict for JsonReader javadoc

* Add JSONReader test cases for U2028 and U2029

* Refactor JSONReader#peekKeyWord() based on @eamonmcmanus's suggestion

* Deprecate setLenient in favor of setStrictness

---------

Co-authored-by: Carl Peterson <unknown>
Co-authored-by: Gustaf Johansson <gustajoh@kth.se>
Co-authored-by: gustajoh <58432871+gustajoh@users.noreply.github.com>
Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Strictness follow-up (#2408)

* Strictness mode follow-up

- Remove mentions of `null` Gson strictness; this is an implementation detail
- Fix incorrect / outdated documentation
- Reduce links to RFC; if there is already a link to it in a previous sentence
  don't link to it again
- Extend and update tests
- Minor punctuation changes in documentation for consistency

* Deprecate `setLenient` methods

* `strictness2` fixes & improvements (#2456)

* Adjust ProGuard default rules and shrinking tests (#2420)

* Adjust ProGuard default rules and shrinking tests

* Adjust comment

* Add shrinking test for class without no-args constructor; improve docs

* Improve Unsafe mention in Troubleshooting Guide

* Improve comment for `-if class *`

* Bump com.google.guava:guava from 32.0.1-jre to 32.1.1-jre (#2444)

Bumps [com.google.guava:guava](https://github.com/google/guava) from 32.0.1-jre to 32.1.1-jre.
- [Release notes](https://github.com/google/guava/releases)
- [Commits](https://github.com/google/guava/commits)

---
updated-dependencies:
- dependency-name: com.google.guava:guava
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump com.google.guava:guava-testlib from 32.0.1-jre to 32.1.1-jre (#2443)

Bumps [com.google.guava:guava-testlib](https://github.com/google/guava) from 32.0.1-jre to 32.1.1-jre.
- [Release notes](https://github.com/google/guava/releases)
- [Commits](https://github.com/google/guava/commits)

---
updated-dependencies:
- dependency-name: com.google.guava:guava-testlib
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Support non-generic type for `TypeToken.getParameterized` for legacy reasons (#2447)

This partially restores the behavior before a589ef2,
except that back then for a non-generic type a bogus `TypeToken(ParameterizedType)`
was created, whereas now a `TypeToken(Class)` is created instead.

* Fixed Typo in GsonBuilder.java (#2449)

* Make date-formatting tests less fragile with regular expressions. (#2450)

* Make date-formatting tests less fragile with regular expressions.

This is not great. We should really ensure that formatted dates are the same
regardless of JDK version. There is code that attempts to do that but it is not
really effective. So for now we fudge around the differences by using regular
expressions to paper over the differences.

* Temporarily add test-debugging code.

* Another attempt at debugging a test failure.

* Fix pattern in assertion.

* Modification in test cases (#2454)

* Fixed Typo in GsonBuilder.java

* Suggestions on Test cases

* Modified test cases using assertThrows method (JUnit)

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/GsonTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

---------

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Minor follow-up changes

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: elevne <97422844+elevne@users.noreply.github.com>
Co-authored-by: Éamonn McManus <emcmanus@google.com>
Co-authored-by: Wonil <cwi5525@naver.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Marten <martenvoorberg@gmail.com>
Co-authored-by: Gustaf Johansson <gustajoh@kth.se>
Co-authored-by: gustajoh <58432871+gustajoh@users.noreply.github.com>
Co-authored-by: LMC117 <2295699210@qq.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: elevne <97422844+elevne@users.noreply.github.com>
Co-authored-by: Wonil <cwi5525@naver.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant