Search in sources :

Example 86 with GsonBuilder

use of com.google.gson.GsonBuilder in project instructure-android by instructure.

the class CanvasRestAdapter method buildAdapterSerializeNulls.

public Retrofit buildAdapterSerializeNulls(@NonNull RestParams params) {
    if (params.getDomain() == null || params.getDomain().length() == 0) {
        params = new RestParams.Builder(params).withDomain(ApiPrefs.getFullDomain()).build();
    }
    if (mCallback != null) {
        mCallback.onCallbackStarted();
    }
    // Can make this check as we KNOW that the setter doesn't allow empty strings.
    if (params.getDomain().equals("")) {
        Logger.d("The RestAdapter hasn't been set up yet. Call setupInstance(context,token,domain)");
        return new Retrofit.Builder().baseUrl("http://invalid.domain.com/").build();
    }
    String apiContext = "";
    if (params.getCanvasContext() != null) {
        if (params.getCanvasContext().getType() == CanvasContext.Type.COURSE) {
            apiContext = "courses/";
        } else if (params.getCanvasContext().getType() == CanvasContext.Type.GROUP) {
            apiContext = "groups/";
        } else if (params.getCanvasContext().getType() == CanvasContext.Type.SECTION) {
            apiContext = "sections/";
        } else {
            apiContext = "users/";
        }
    }
    // Sets the auth token, user agent, and handles masquerading.
    final RestParams restParams = params;
    return new Retrofit.Builder().baseUrl(params.getDomain() + params.getAPIVersion() + apiContext).addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create())).callFactory(new Call.Factory() {

        @Override
        public Call newCall(Request request) {
            // Tag this request with the rest params so we can access them later in RequestInterceptor
            request = request.newBuilder().tag(restParams).build();
            return getOkHttpClient().newCall(request);
        }
    }).build();
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory)

Example 87 with GsonBuilder

use of com.google.gson.GsonBuilder in project instructure-android by instructure.

the class ScheduleItemTest method getIdTest_AssignmentOverrides.

@Test
public void getIdTest_AssignmentOverrides() throws Exception {
    // can't set a schedule item's id to be a string by any exposed method, so use JSON parsing
    // to test it
    String scheduleItemJSON = "{\n\"all_day\": true,\n\"all_day_date\": \"2012-10-17\",\n\"created_at\": \"2012-10-06T01:09:52Z\",\n\"end_at\": \"2012-10-17T06:00:00Z\",\n\"id\": \"assignment_673956\",\n\"location_address\": null,\n\"location_name\": null,\n\"start_at\": \"2012-10-17T06:00:00Z\",\n\"title\": \"No Class\",\n\"updated_at\": \"2012-10-06T01:09:52Z\",\n\"workflow_state\": \"active\",\n\"description\": null,\n\"context_code\": \"course_833052\",\n\"child_events_count\": 0,\n\"parent_event_id\": null,\n\"hidden\": false,\n\"child_events\": [],\n\"url\": \"https://mobiledev.instructure.com/api/v1/calendar_events/673956\",\n\"html_url\": \"https://mobiledev.instructure.com/calendar?event_id=673956&include_contexts=course_833052#7b2273686f77223a2267726f75705f636f757273655f383333303532227d\"\n}";
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    ScheduleItem scheduleItem = gson.fromJson(scheduleItemJSON, ScheduleItem.class);
    AssignmentOverride assignmentOverride = new AssignmentOverride();
    assignmentOverride.setId(1234567);
    ArrayList<AssignmentOverride> assignmentOverrides = new ArrayList<>();
    assignmentOverrides.add(assignmentOverride);
    scheduleItem.setAssignmentOverrides(assignmentOverrides);
    assertEquals(1234567, scheduleItem.getId());
}
Also used : ScheduleItem(com.instructure.canvasapi2.models.ScheduleItem) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) AssignmentOverride(com.instructure.canvasapi2.models.AssignmentOverride) Test(org.junit.Test)

Example 88 with GsonBuilder

use of com.google.gson.GsonBuilder in project Terasology by MovingBlocks.

the class BehaviorTreeBuilder method initGson.

private void initGson() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeHierarchyAdapter(BehaviorNode.class, this);
        gsonBuilder.registerTypeAdapterFactory(new UriTypeAdapterFactory());
        gsonBuilder.registerTypeAdapter(BehaviorTree.class, new TypeAdapter<BehaviorTree>() {

            @Override
            public void write(JsonWriter out, BehaviorTree value) throws IOException {
                if (value != null) {
                    // TODO doublecheck URN
                    out.value(value.getUrn().toString());
                } else {
                    out.value("");
                }
            }

            @Override
            public BehaviorTree read(JsonReader in) throws IOException {
                String uri = in.nextString();
                AssetManager assetManager = CoreRegistry.get(AssetManager.class);
                return assetManager.getAsset(new ResourceUrn(uri), BehaviorTree.class).orElse(assetManager.getAsset(new ResourceUrn("Behaviors:fallback"), BehaviorTree.class).get());
            }
        });
        gson = gsonBuilder.create();
    }
}
Also used : AssetManager(org.terasology.assets.management.AssetManager) GsonBuilder(com.google.gson.GsonBuilder) UriTypeAdapterFactory(org.terasology.utilities.gson.UriTypeAdapterFactory) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) BehaviorTree(org.terasology.logic.behavior.asset.BehaviorTree) ResourceUrn(org.terasology.assets.ResourceUrn) JsonWriter(com.google.gson.stream.JsonWriter)

Example 89 with GsonBuilder

use of com.google.gson.GsonBuilder in project Terasology by MovingBlocks.

the class CountCallsTest method init.

@Before
public void init() {
    constructCalled.clear();
    destructCalled.clear();
    executeCalled.clear();
    nextId2 = 1;
    gsonBuilder = new GsonBuilder();
    BehaviorTreeBuilder builder = new BehaviorTreeBuilder() {

        @Override
        public BehaviorNode createNode(BehaviorNode node) {
            return new CountDelegate(node);
        }
    };
    gsonBuilder.registerTypeAdapter(BehaviorNode.class, builder);
// gsonBuilder.registerTypeAdapter(Action.class, new InheritanceAdapter<Action>("delay", Delay.class));
}
Also used : BehaviorTreeBuilder(org.terasology.logic.behavior.core.BehaviorTreeBuilder) GsonBuilder(com.google.gson.GsonBuilder) BehaviorNode(org.terasology.logic.behavior.core.BehaviorNode) Before(org.junit.Before)

Example 90 with GsonBuilder

use of com.google.gson.GsonBuilder in project carbondata by apache.

the class Schema method parseJson.

/**
 * Create a Schema using JSON string, for example:
 * [
 *   {"name":"string"},
 *   {"age":"int"}
 * ]
 */
public static Schema parseJson(String json) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Field.class, new TypeAdapter<Field>() {

        @Override
        public void write(JsonWriter out, Field field) throws IOException {
        // noop
        }

        @Override
        public Field read(JsonReader in) throws IOException {
            in.beginObject();
            Field field = new Field(in.nextName(), in.nextString());
            in.endObject();
            return field;
        }
    });
    Field[] fields = gsonBuilder.create().fromJson(json, Field[].class);
    return new Schema(fields);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)1067 Gson (com.google.gson.Gson)803 IOException (java.io.IOException)185 Test (org.junit.Test)141 ArrayList (java.util.ArrayList)101 JsonObject (com.google.gson.JsonObject)90 File (java.io.File)80 JsonElement (com.google.gson.JsonElement)78 HashMap (java.util.HashMap)67 List (java.util.List)62 Map (java.util.Map)59 Retrofit (retrofit2.Retrofit)56 Type (java.lang.reflect.Type)52 FileNotFoundException (java.io.FileNotFoundException)42 TypeToken (com.google.gson.reflect.TypeToken)40 ResponseBody (okhttp3.ResponseBody)39 FileOutputStream (java.io.FileOutputStream)38 Call (retrofit2.Call)38 JsonSyntaxException (com.google.gson.JsonSyntaxException)37 JsonParser (com.google.gson.JsonParser)36