Codebase list libgoogle-gson-java / 8bc8193
New upstream version 2.8.4 Emmanuel Bourg 5 years ago
16 changed file(s) with 188 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
1212 *.iws
1313 classes
1414
15 .gradle
16 local.properties
17 build
18
1519 .DS_Store
00 Change Log
11 ==========
2
3 ## Version 2.8.3
4 _2018-04-27_ [GitHub Diff](https://github.com/google/gson/compare/gson-parent-2.8.2...gson-parent-2.8.3)
5 * Added a new API, `GsonBuilder.newBuilder()` that clones the current builder
6 * Preserving DateFormatter behavior on JDK 9
7 * Numerous other bugfixes
28
39 ## Version 2.8.2
410 _2017-09-19_ [GitHub Diff](https://github.com/google/gson/compare/gson-parent-2.8.1...gson-parent-2.8.2)
1919 * To use Gson in Android
2020 ```gradle
2121 dependencies {
22 compile 'com.google.code.gson:gson:2.8.2'
22 compile 'com.google.code.gson:gson:2.8.3'
2323 }
2424 ```
2525
2929 <dependency>
3030 <groupId>com.google.code.gson</groupId>
3131 <artifactId>gson</artifactId>
32 <version>2.8.2</version>
32 <version>2.8.3</version>
3333 </dependency>
3434 ```
3535
0 #Gson Release Process
0 # Gson Release Process
11
22 The following is a step-by-step procedure for releasing a new version of Google-Gson.
33
99 1. Run `mvn release:clean`
1010 1. Do a dry run: `mvn release:prepare -DdryRun=true`
1111 1. Start the release: `mvn release:prepare`
12 * Answer questions: usually the defaults are fine.
13 * This will do a full build, change version from `-SNAPSHOT` to the released version, commit and create the tags. It will then change the version to `-SNAPSHOT` for the next release.
12 * Answer questions: usually the defaults are fine.
13 * This will do a full build, change version from `-SNAPSHOT` to the released version, commit and create the tags. It will then change the version to `-SNAPSHOT` for the next release.
1414 1. Ensure you have defined `sonatype-nexus-staging` in your Maven `settings.xml` and run:
1515
1616 ```bash
1919
2020 1. [Log in to Nexus repository manager](https://oss.sonatype.org/index.html#welcome) at Sonatype and close the staging repository for Gson. If you run into an error regarding missing signatures, you need to manually upload the artifacts using `mvn gpg:sign-and-deploy-file` for Gson binary, source and Javadoc jars.
2121
22 ```bash
23 cp -r ~/.m2/repository/com/google/code/gson/gson/1.7.2 /tmp
24 cd /tmp/1.7.2
22 ```bash
23 cp -r ~/.m2/repository/com/google/code/gson/gson/1.7.2 /tmp
24 cd /tmp/1.7.2
2525
26 mvn gpg:sign-and-deploy-file \
26 mvn gpg:sign-and-deploy-file \
2727 -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ \
2828 -DrepositoryId=sonatype-nexus-staging \
2929 -DpomFile=gson-1.7.2.pom \
3030 -Dfile=gson-1.7.2-javadoc.jar \
3131 -Dclassifier=javadoc
3232
33 mvn gpg:sign-and-deploy-file \
33 mvn gpg:sign-and-deploy-file \
3434 -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ \
3535 -DrepositoryId=sonatype-nexus-staging \
3636 -DpomFile=gson-1.7.2.pom \
3737 -Dfile=gson-1.7.2-sources.jar \
3838 -Dclassifier=sources
3939
40 mvn gpg:sign-and-deploy-file \
40 mvn gpg:sign-and-deploy-file \
4141 -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ \
4242 -DrepositoryId=sonatype-nexus-staging \
4343 -DpomFile=gson-1.7.2.pom \
4444 -Dfile=gson-1.7.2.jar
45 ```
45 ```
4646
4747 1. Close the Gson repository. Download and sanity check all downloads. Do not skip this step! Once you release the staging repository, there is no going back. It will get synced with Maven central and you will not be able to update or delete anything. Your only recourse will be to release a new version of Gson and hope that no one uses the old one.
4848 1. Release the staging repository for Gson. Gson will now get synced to Maven central with-in the next hour. For issues consult [Sonatype Guide](https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide#SonatypeOSSMavenRepositoryUsageGuide-8.ReleaseIt).
5757 This section was borrowed heavily from [Doclava release process](http://code.google.com/p/doclava/wiki/ProcessRelease).
5858
5959 1. Install/Configure GPG following this [guide](http://www.sonatype.com/people/2010/01/how-to-generate-pgp-signatures-with-maven/).
60 2. [Create encrypted passwords](http://maven.apache.org/guides/mini/guide-encryption.html).
61 3. Create `~/.m2/settings.xml` similar to as described in [Doclava release process](https://code.google.com/p/doclava/wiki/ProcessRelease).
62 4. Now for deploying a snapshot repository, use `mvn deploy`.
60 1. [Create encrypted passwords](http://maven.apache.org/guides/mini/guide-encryption.html).
61 1. Create `~/.m2/settings.xml` similar to as described in [Doclava release process](https://code.google.com/p/doclava/wiki/ProcessRelease).
62 1. Now for deploying a snapshot repository, use `mvn deploy`.
6363
6464 ## Getting Maven Publishing Privileges
6565
6666 Based on [Gson group thread](https://groups.google.com/d/topic/google-gson/DHWJHVFpIBg/discussion):
6767
6868 1. [Sign up for a Sonatype account](https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide) following instructions under (2) on that page
69 2. Ask one of the existing members of the repository to create a JIRA ticket (Step 3 of above document) to add you to the publisher list.
69 1. Ask one of the existing members of the repository to create a JIRA ticket (Step 3 of above document) to add you to the publisher list.
7070
7171 ## Running Benchmarks or Tests on Android
7272
7373 ## <a name="TOC-Gson-With-Gradle"></a>Using Gson with Gradle/Android
7474 ```
7575 dependencies {
76 compile 'com.google.code.gson:gson:2.8.2'
76 compile 'com.google.code.gson:gson:2.8.3'
7777 }
7878 ```
7979 ## <a name="TOC-Gson-With-Maven"></a>Using Gson with Maven
8585 <dependency>
8686 <groupId>com.google.code.gson</groupId>
8787 <artifactId>gson</artifactId>
88 <version>2.8.2</version>
88 <version>2.8.3</version>
8989 <scope>compile</scope>
9090 </dependency>
9191 </dependencies>
0 buildscript {
1 repositories {
2 mavenCentral()
3 }
4 }
5
6 allprojects {
7 repositories {
8 mavenCentral()
9 }
10 }
11
0 #Fri Apr 27 17:41:01 PDT 2018
1 distributionBase=GRADLE_USER_HOME
2 distributionPath=wrapper/dists
3 zipStoreBase=GRADLE_USER_HOME
4 zipStorePath=wrapper/dists
5 distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
0 apply plugin: 'java'
1 apply plugin: 'maven'
2
3 group = 'com.google.code.gson'
4 version = '2.8.4-SNAPSHOT'
5
6 sourceCompatibility = 1.6
7 targetCompatibility = 1.6
8
9 sourceSets.main.java.exclude("**/module-info.java")
10 dependencies {
11 testCompile "junit:junit:4.12"
12 }
33 <parent>
44 <groupId>com.google.code.gson</groupId>
55 <artifactId>gson-parent</artifactId>
6 <version>2.8.3</version>
6 <version>2.8.4</version>
77 </parent>
88
99 <artifactId>gson</artifactId>
113113 @Override public String translateName(Field f) {
114114 return separateCamelCase(f.getName(), "-").toLowerCase(Locale.ENGLISH);
115115 }
116 },
117
118 /**
119 * Using this naming policy with Gson will modify the Java Field name from its camel cased
120 * form to a lower case field name where each word is separated by a dot (.).
121 *
122 * <p>Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":</p>
123 * <ul>
124 * <li>someFieldName ---> some.field.name</li>
125 * <li>_someFieldName ---> _some.field.name</li>
126 * <li>aStringField ---> a.string.field</li>
127 * <li>aURL ---> a.u.r.l</li>
128 * </ul>
129 * Using dots in JavaScript is not recommended since dot is also used for a member sign in
130 * expressions. This requires that a field named with dots is always accessed as a quoted
131 * property like {@code myobject['my.field']}. Accessing it as an object field
132 * {@code myobject.my.field} will result in an unintended javascript expression.
133 * @since 2.8
134 */
135 LOWER_CASE_WITH_DOTS() {
136 @Override public String translateName(Field f) {
137 return separateCamelCase(f.getName(), ".").toLowerCase(Locale.ENGLISH);
138 }
116139 };
117140
118141 /**
2424 */
2525 final class PreJava9ReflectionAccessor extends ReflectionAccessor {
2626
27 /**
28 * {@inheritDoc}
29 */
27 /** {@inheritDoc} */
3028 @Override
3129 public void makeAccessible(AccessibleObject ao) {
3230 ao.setAccessible(true);
3331 }
34
3532 }
1414 */
1515 package com.google.gson.internal.reflect;
1616
17 import sun.misc.Unsafe;
18
1917 import java.lang.reflect.AccessibleObject;
2018 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20
21 import com.google.gson.JsonIOException;
2122
2223 /**
2324 * An implementation of {@link ReflectionAccessor} based on {@link Unsafe}.
2526 * NOTE: This implementation is designed for Java 9. Although it should work with earlier Java releases, it is better to
2627 * use {@link PreJava9ReflectionAccessor} for them.
2728 */
29 @SuppressWarnings({"unchecked", "rawtypes"})
2830 final class UnsafeReflectionAccessor extends ReflectionAccessor {
2931
30 private final Unsafe theUnsafe = getUnsafeInstance();
32 private static Class unsafeClass;
33 private final Object theUnsafe = getUnsafeInstance();
3134 private final Field overrideField = getOverrideField();
3235
33 /**
34 * {@inheritDoc}
35 */
36 /** {@inheritDoc} */
3637 @Override
3738 public void makeAccessible(AccessibleObject ao) {
38 if (theUnsafe != null && overrideField != null) {
39 long overrideOffset = theUnsafe.objectFieldOffset(overrideField);
40 theUnsafe.putBoolean(ao, overrideOffset, true);
39 boolean success = makeAccessibleWithUnsafe(ao);
40 if (!success) {
41 try {
42 // unsafe couldn't be found, so try using accessible anyway
43 ao.setAccessible(true);
44 } catch (SecurityException e) {
45 throw new JsonIOException("Gson couldn't modify fields for " + ao
46 + "\nand sun.misc.Unsafe not found.\nEither write a custom type adapter,"
47 + " or make fields accessible, or include sun.misc.Unsafe.", e);
48 }
4149 }
4250 }
4351
44 private static Unsafe getUnsafeInstance() {
52 // Visible for testing only
53 boolean makeAccessibleWithUnsafe(AccessibleObject ao) {
54 if (theUnsafe != null && overrideField != null) {
55 try {
56 Method method = unsafeClass.getMethod("objectFieldOffset", Field.class);
57 long overrideOffset = (Long) method.invoke(theUnsafe, overrideField); // long overrideOffset = theUnsafe.objectFieldOffset(overrideField);
58 Method putBooleanMethod = unsafeClass.getMethod("putBoolean", Object.class, long.class, boolean.class);
59 putBooleanMethod.invoke(theUnsafe, ao, overrideOffset, true); // theUnsafe.putBoolean(ao, overrideOffset, true);
60 return true;
61 } catch (Exception ignored) { // do nothing
62 }
63 }
64 return false;
65 }
66
67 private static Object getUnsafeInstance() {
4568 try {
46 Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
69 unsafeClass = Class.forName("sun.misc.Unsafe");
70 Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
4771 unsafeField.setAccessible(true);
48 return (Unsafe) unsafeField.get(null);
72 return unsafeField.get(null);
4973 } catch (Exception e) {
50 e.printStackTrace();
5174 return null;
5275 }
5376 }
5679 try {
5780 return AccessibleObject.class.getDeclaredField("override");
5881 } catch (NoSuchFieldException e) {
59 e.printStackTrace();
6082 return null;
6183 }
6284 }
6262 + target.someConstantStringInstanceField + "\"}", gson.toJson(target));
6363 }
6464
65 public void testGsonWithLowerCaseDotPolicySerialization() {
66 Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DOTS).create();
67 StringWrapper target = new StringWrapper("blah");
68 assertEquals("{\"some.constant.string.instance.field\":\""
69 + target.someConstantStringInstanceField + "\"}", gson.toJson(target));
70 }
71
72 public void testGsonWithLowerCaseDotPolicyDeserialiation() {
73 Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DOTS).create();
74 String target = "{\"some.constant.string.instance.field\":\"someValue\"}";
75 StringWrapper deserializedObject = gson.fromJson(target, StringWrapper.class);
76 assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
77 }
78
6579 public void testGsonWithLowerCaseDashPolicyDeserialiation() {
6680 Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
6781 String target = "{\"some-constant-string-instance-field\":\"someValue\"}";
0 /*
1 * Copyright (C) 2018 The Gson authors
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package com.google.gson.internal.reflect;
16
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.lang.reflect.Field;
21
22 import org.junit.Test;
23
24 /**
25 * Unit tests for {@link UnsafeReflectionAccessor}
26 *
27 * @author Inderjeet Singh
28 */
29 public class UnsafeReflectionAccessorTest {
30
31 @Test
32 public void testMakeAccessibleWithUnsafe() throws Exception {
33 UnsafeReflectionAccessor accessor = new UnsafeReflectionAccessor();
34 Field field = ClassWithPrivateFinalFields.class.getDeclaredField("a");
35 try {
36 boolean success = accessor.makeAccessibleWithUnsafe(field);
37 assertTrue(success);
38 } catch (Exception e) {
39 fail("Unsafe didn't work on the JDK");
40 }
41 }
42
43 @SuppressWarnings("unused")
44 private static final class ClassWithPrivateFinalFields {
45 private final String a;
46 public ClassWithPrivateFinalFields(String a) {
47 this.a = a;
48 }
49 }
50 }
1010
1111 <groupId>com.google.code.gson</groupId>
1212 <artifactId>gson-parent</artifactId>
13 <version>2.8.3</version>
13 <version>2.8.4</version>
1414 <packaging>pom</packaging>
1515
1616 <name>Gson Parent</name>
3030 <url>https://github.com/google/gson/</url>
3131 <connection>scm:git:https://github.com/google/gson.git</connection>
3232 <developerConnection>scm:git:git@github.com:google/gson.git</developerConnection>
33 <tag>gson-parent-2.8.3</tag>
33 <tag>gson-parent-2.8.4</tag>
3434 </scm>
3535
3636 <issueManagement>
0 include ':gson'