### Question:
CdrSerialiser implements JsonSerializer<Cdr>, JsonDeserializer<Cdr> { JsonElement cdrItemToJsonElement(CdrItem cdrItem) { switch (cdrItem.getType()) { case CDR_STRING: return new JsonPrimitive(cdrItem.getStrval()); case CDR_INTEGER: return new JsonPrimitive(cdrItem.getIntval()); case CDR_DOUBLE: return new JsonPrimitive(cdrItem.getDoubleval()); case CDR_BOOLEAN: return new JsonPrimitive(cdrItem.getBoolVal()); case CDR_ARRAY: List<Cdr> childCdrs = cdrItem.getCdrs(); boolean isMap = !childCdrs.get(0).containsKey("0"); if (isMap) { return getMap(childCdrs); } else { return getArray(childCdrs); } default: return JsonNull.INSTANCE; } }  @Override JsonElement serialize(Cdr src, Type typeOfSrc, JsonSerializationContext context); @Override Cdr deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context);  }### Answer:
@Test public void testCdrItemToJsonElementPrimitives() { CdrSerialiser cs = new CdrSerialiser(); CdrItem booleanCdr = new CdrItem(CdrItem.CdrItemType.CDR_BOOLEAN); booleanCdr.setBoolVal(true); JsonElement boolElement = cs.cdrItemToJsonElement(booleanCdr); assertTrue(boolElement instanceof JsonPrimitive); assertTrue(boolElement.getAsBoolean()); CdrItem stringCdr = new CdrItem(CdrItem.CdrItemType.CDR_STRING); stringCdr.setStrval("testString"); JsonElement stringElement = cs.cdrItemToJsonElement(stringCdr); assertTrue(stringElement instanceof JsonPrimitive); assertEquals("testString", stringElement.getAsString()); CdrItem intCdr = new CdrItem(CdrItem.CdrItemType.CDR_INTEGER); intCdr.setIntval(2); JsonElement intElement = cs.cdrItemToJsonElement(intCdr); assertTrue(intElement instanceof JsonPrimitive); assertEquals(2, intElement.getAsInt()); CdrItem doubleCdr = new CdrItem(CdrItem.CdrItemType.CDR_DOUBLE); doubleCdr.setDoubleval(2.23); JsonElement doubleElement = cs.cdrItemToJsonElement(doubleCdr); assertTrue(doubleElement instanceof JsonPrimitive); assertEquals(2.23, doubleElement.getAsDouble(), 0); }
@Test public void testCdrItemToJsonElementArray() { CdrSerialiser cs = new CdrSerialiser(); CdrItem arrayCdrItem = new CdrItem(CdrItem.CdrItemType.CDR_ARRAY); List<Cdr> arrayChildren = new ArrayList<>(); for (int i = 0; i < 5; i++) { Cdr cdr = new Cdr(""); cdr.set("" + i, i); arrayChildren.add(cdr); } arrayCdrItem.setCdrs(arrayChildren); JsonElement arrayElement = cs.cdrItemToJsonElement(arrayCdrItem); assertTrue(arrayElement instanceof JsonArray); JsonArray castArray = (JsonArray) arrayElement; arrayChildren = new ArrayList<>(); assertEquals(5, castArray.size()); for (int i = 0; i < 5; i++) { assertEquals(new JsonPrimitive(i), castArray.get(i)); Cdr cdr = new Cdr(""); cdr.set("test" + i, i); arrayChildren.add(cdr); } arrayCdrItem.setCdrs(arrayChildren); JsonElement mapElement = cs.cdrItemToJsonElement(arrayCdrItem); assertTrue(mapElement instanceof JsonObject); JsonObject castObject = (JsonObject) mapElement; assertEquals(5, castObject.size()); for (int i = 0; i < 5; i++) { assertTrue(castObject.has("test" + i)); assertEquals("" + i, castObject.get("test" + i).getAsString()); } }