Skip to content

Instantly share code, notes, and snippets.

@senneco
Created October 22, 2015 09:29
Show Gist options
  • Save senneco/407d3019f37cae76a81f to your computer and use it in GitHub Desktop.
Save senneco/407d3019f37cae76a81f to your computer and use it in GitHub Desktop.
private static class InlineConverter extends GsonConverter
{
private static String sGuid = null;
private static final String CHARSET = "UTF-8";
private final RequestHeader mRequestHeader;
private final DeviceGuid mDeviceGuid;
private final Gson mGson;
public InlineConverter(Gson gson, String appVersion)
{
super(gson, CHARSET);
mGson = gson;
mRequestHeader = new RequestHeader();
mRequestHeader.setCultureCode("en_GB");
mRequestHeader.setPlatform("Android1");
mRequestHeader.setVersion(appVersion);
mRequestHeader.setTimeZoneOffset((int) (TimeZone.getDefault().getRawOffset() / TimeUnit.HOURS.toMillis(1)));
if (sGuid == null)
{
sGuid = UUID.randomUUID().toString();
}
mDeviceGuid = new DeviceGuid();
mDeviceGuid.setDeviceGuid(UUID.randomUUID().toString());
}
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException
{
final Type[] genericInterfaces = ((Class) type).getGenericInterfaces();
if (!Arrays.asList(genericInterfaces).contains(IServiceResponseData.class))
{
return super.fromBody(body, type);
}
InputStreamReader isr = null;
final JsonObject jsonObject;
try
{
isr = new InputStreamReader(body.in(), CHARSET);
jsonObject = new JsonParser().parse(new JsonReader(isr)).getAsJsonObject();
}
catch (Exception e)
{
throw new ConversionException(e);
}
finally
{
if (isr != null)
{
try
{
isr.close();
}
catch (IOException ignored)
{
}
}
}
List<String> deviceGuidKeys = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : mGson.toJsonTree(new DeviceGuid()).getAsJsonObject().entrySet())
{
deviceGuidKeys.add(entry.getKey());
}
List<String> headerKeys = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : mGson.toJsonTree(new ResponseHeader()).getAsJsonObject().entrySet())
{
headerKeys.add(entry.getKey());
}
JsonObject deviceGuidObject = new JsonObject();
for (String deviceGuidKey : deviceGuidKeys)
{
deviceGuidObject.add(deviceGuidKey, jsonObject.remove(deviceGuidKey));
}
JsonObject headerObject = new JsonObject();
for (String headerKey : headerKeys)
{
headerObject.add(headerKey, jsonObject.remove(headerKey));
}
jsonObject.add("DeviceGuid", deviceGuidObject);
jsonObject.add("Header", headerObject);
return mGson.fromJson(jsonObject, type);
}
@Override
public TypedOutput toBody(Object object)
{
if (!(object instanceof IServiceRequestData))
{
return super.toBody(object);
}
((IServiceRequestData) object).setDeviceGuid(mDeviceGuid);
((IServiceRequestData) object).setHeader(mRequestHeader);
final JsonObject request = mGson.toJsonTree(object).getAsJsonObject();
//TODO: lets use annotation like @Inline
Set<Map.Entry<String, JsonElement>> inlineItems = new HashSet<>();
inlineItems.addAll(request.getAsJsonObject("DeviceGuid").entrySet());
inlineItems.addAll(request.getAsJsonObject("Header").entrySet());
request.remove("DeviceGuid");
request.remove("Header");
for (Map.Entry<String, JsonElement> entry : inlineItems)
{
request.add(entry.getKey(), entry.getValue());
}
JsonObject wrappedRequest = new JsonObject();
wrappedRequest.add("request", request);
byte[] jsonBytes;
try
{
jsonBytes = mGson.toJson(wrappedRequest).getBytes(CHARSET);
}
catch (UnsupportedEncodingException e)
{
jsonBytes = new byte[0];
}
return new JsonTypedOutput(jsonBytes, CHARSET);
}
private static class JsonTypedOutput implements TypedOutput
{
private final byte[] jsonBytes;
private final String mimeType;
JsonTypedOutput(byte[] jsonBytes, String encode)
{
this.jsonBytes = jsonBytes;
this.mimeType = "application/json; charset=" + encode;
}
@Override
public String fileName()
{
return null;
}
@Override
public String mimeType()
{
return mimeType;
}
@Override
public long length()
{
return jsonBytes.length;
}
@Override
public void writeTo(OutputStream out) throws IOException
{
out.write(jsonBytes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment