Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gmetal committed Dec 18, 2017
0 parents commit bb637ba
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.gradle/
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (C) 2017 - gmetal <gmetaxas@gmail.com>

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.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jsoniter (json-iterator) Converter
=================

A `Converter` which uses [jsoniter][1] for serialization to and from JSON.

The default jsoniter decoding mode is DecodingMode.REFLECTION_MODE. You can however
use another DecodingMode, as provided by the jsoniter library.

You can use [jitpack][2] to use the library.

```
compile 'com.github.gmetal:converter-jsoniter:latest.version'
```

[1]: http://jsoniter.com/
[2]: https://jitpack.io/

21 changes: 21 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
group 'com.github.gmetal'
version '0.1-SNAPSHOT'

buildscript {
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}

dependencies{
classpath 'gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.14.0'
}
}

allprojects {
repositories {
jcenter()
}
}

apply plugin: 'com.github.hierynomus.license'
1 change: 1 addition & 0 deletions converter-jsoniter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
13 changes: 13 additions & 0 deletions converter-jsoniter/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apply plugin: 'java-library'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.jsoniter:jsoniter:0.9.18'
// https://mvnrepository.com/artifact/org.javassist/javassist
compile group: 'org.javassist', name: 'javassist', version: '3.22.0-GA'
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
36 changes: 36 additions & 0 deletions converter-jsoniter/converter-jsoniter.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":converter-jsoniter" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$USER_HOME$/development/Android/Projects/Phabdroid" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":converter-jsoniter" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="true" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/build/classes/java/main" />
<output-test url="file://$MODULE_DIR$/build/classes/java/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="PROVIDED" name="retrofit-2.3.0" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="jsoniter-0.9.18" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="javassist-3.22.0-GA" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="okhttp-3.8.0" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="okio-1.13.0" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (C) 2017 - gmetal <gmetaxas@gmail.com>
*
* 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.
*/
package gr.gmetal.jsoniter;

import com.jsoniter.JsonIterator;
import com.jsoniter.spi.DecodingMode;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class JsoniterConverterFactory extends Converter.Factory {

public static JsoniterConverterFactory create() {

return new JsoniterConverterFactory(DecodingMode.REFLECTION_MODE);
}

public static JsoniterConverterFactory create(final DecodingMode decodingMode) {

return new JsoniterConverterFactory(decodingMode);
}

private JsoniterConverterFactory(final DecodingMode decodingMode) {

JsonIterator.setMode(decodingMode);
}

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {

return new JsoniterResponseBodyConverter<>((Class<?>) type);
}

@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations,
Annotation[] methodAnnotations,
Retrofit retrofit) {

return new JsoniterRequestBodyConverter<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (C) 2017 - gmetal <gmetaxas@gmail.com>
*
* 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.
*/
package gr.gmetal.jsoniter;

import com.jsoniter.output.JsonStream;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Converter;

class JsoniterRequestBodyConverter<T> implements Converter<T, RequestBody> {

private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");

@Override
public RequestBody convert(final T value) throws IOException {

return RequestBody.create(MEDIA_TYPE, JsonStream.serialize(value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (C) 2017 - gmetal <gmetaxas@gmail.com>
*
* 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.
*/
package gr.gmetal.jsoniter;

import com.jsoniter.JsonIterator;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Converter;

class JsoniterResponseBodyConverter<T> implements Converter<ResponseBody, T> {

public static final int BUF_SIZE = 512;

private Class<T> mCls;

public JsoniterResponseBodyConverter(Class<T> cls) {

mCls = cls;
}

@Override
public T convert(final ResponseBody value) throws IOException {

final JsonIterator parsedValue = JsonIterator.parse(value.byteStream(), BUF_SIZE);

return parsedValue.read(mCls);
}
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Mon Dec 18 19:25:31 EET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-bin.zip
Loading

0 comments on commit bb637ba

Please sign in to comment.