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();
}
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());
}
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();
}
}
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));
}
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);
}
Aggregations