@Override public JSONObject toJsonObject() throws JSONException { JSONObject ret = super.toJsonObject(); ret.put(KEY_TIMESTAMP, timestamp); return ret; } }
protected ExtendedData(String json) throws JSONException { JSONObject jsonObject = new JSONObject(json); setVersion(jsonObject.optInt(KEY_VERSION, -1)); init(); }
public EventPayload(String label, String interactionId, String data, Map<String, Object> customData, ExtendedData... extendedData) { super(PayloadType.event); try { put(KEY_LABEL, label); if (interactionId != null) { put(KEY_INTERACTION_ID, interactionId); } if (data != null) { put(KEY_DATA, new JSONObject(data)); } if (customData != null && !customData.isEmpty()) { JSONObject customDataJson = generateCustomDataJson(customData); put(KEY_CUSTOM_DATA, customDataJson); } if (extendedData != null && extendedData.length != 0) { for (ExtendedData currentExtendedData : extendedData) { if (currentExtendedData != null) { put(currentExtendedData.getTypeName(), currentExtendedData.toJsonObject()); } } } } catch (JSONException e) { ApptentiveLog.e(e, "Unable to construct Event."); logException(e); } }
protected ExtendedData() { init(); }
@Override public JSONObject toJsonObject() throws JSONException { JSONObject ret = super.toJsonObject(); JSONArray coordinates = new JSONArray(); ret.put(KEY_COORDINATES, coordinates); coordinates.put(longitude); coordinates.put(latitude); return ret; } }
@Override public JSONObject toJsonObject() throws JSONException { JSONObject ret = super.toJsonObject(); ret.put(KEY_ID, id); ret.put(KEY_AFFILIATION, affiliation); ret.put(KEY_REVENUE, revenue); ret.put(KEY_SHIPPING, shipping); ret.put(KEY_TAX, tax); ret.put(KEY_CURRENCY, currency); JSONArray itemsArray = new JSONArray(); for (Item item : items) { itemsArray.put(item.toJsonObject()); } ret.put(KEY_ITEMS, itemsArray); return ret; }
@Test public void timeExtendedData() throws JSONException { ApptentiveLog.e("testTimeExtendedData()"); ExtendedData expected = new TimeExtendedData(loadTextAssetAsString(TEST_DATA_DIR + "testTimeExtendedData.json")); TimeExtendedData millis = new TimeExtendedData(1406251926165L); ApptentiveLog.e("expected: %s\n\n millis: %s", expected.toJsonObject().toString(), millis.toJsonObject().toString()); assertTrue(JsonDiffer.areObjectsEqual(expected.toJsonObject(), millis.toJsonObject())); TimeExtendedData seconds = new TimeExtendedData(1406251926.165); assertTrue(JsonDiffer.areObjectsEqual(expected.toJsonObject(), seconds.toJsonObject())); TimeExtendedData date = new TimeExtendedData(new Date(1406251926165L)); assertTrue(JsonDiffer.areObjectsEqual(expected.toJsonObject(), date.toJsonObject())); } }
@Test public void locationExtendedData() throws JSONException { ApptentiveLog.e("testLocationExtendedData()"); ExtendedData expected = new LocationExtendedData(loadTextAssetAsString(TEST_DATA_DIR + "testLocationExtendedData.json")); LocationExtendedData actual = new LocationExtendedData(-122.34569190000002d, 47.6288591d); assertEquals(expected.toJsonObject().toString(), actual.toJsonObject().toString()); }
@Test public void commerceExtendedData() throws JSONException { ApptentiveLog.e("testCommerceExtendedData()"); ExtendedData expected = new CommerceExtendedData(loadTextAssetAsString(TEST_DATA_DIR + "testCommerceExtendedData.json")); CommerceExtendedData actual = new CommerceExtendedData() .setId("commerce_id") .setAffiliation("1111111111") .setRevenue(100d) .setShipping(5L) .setTax(4.38f) .setCurrency("USD"); CommerceExtendedData.Item item = new CommerceExtendedData.Item(22222222, "Item Name", "Category", 20, 5.0d, "USD"); actual.addItem(item); assertTrue(JsonDiffer.areObjectsEqual(expected.toJsonObject(), actual.toJsonObject())); }