Java Code Examples for android.util.JsonWriter#endObject()

The following examples show how to use android.util.JsonWriter#endObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonUtils.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
public static String listToJsonStr(final List<Object> list) {
    if (list == null || list.isEmpty()) {
        return EMPTY_STRING;
    }
    final StringWriter sw = new StringWriter();
    final JsonWriter writer = new JsonWriter(sw);
    try {
        writer.beginArray();
        for (final Object o : list) {
            writer.beginObject();
            if (o instanceof Integer) {
                writer.name(INTEGER_CLASS_NAME).value((Integer)o);
            } else if (o instanceof String) {
                writer.name(STRING_CLASS_NAME).value((String)o);
            }
            writer.endObject();
        }
        writer.endArray();
        return sw.toString();
    } catch (final IOException e) {
    } finally {
        close(writer);
    }
    return EMPTY_STRING;
}
 
Example 2
Source File: JsonUtils.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
public static String listToJsonStr(final List<Object> list) {
    if (list == null || list.isEmpty()) {
        return EMPTY_STRING;
    }
    final StringWriter sw = new StringWriter();
    final JsonWriter writer = new JsonWriter(sw);
    try {
        writer.beginArray();
        for (final Object o : list) {
            writer.beginObject();
            if (o instanceof Integer) {
                writer.name(INTEGER_CLASS_NAME).value((Integer)o);
            } else if (o instanceof String) {
                writer.name(STRING_CLASS_NAME).value((String)o);
            }
            writer.endObject();
        }
        writer.endArray();
        return sw.toString();
    } catch (final IOException e) {
    } finally {
        close(writer);
    }
    return EMPTY_STRING;
}
 
Example 3
Source File: AnswerScreenState.java    From androdns with Apache License 2.0 6 votes vote down vote up
public void toJSON(JsonWriter writer) throws IOException {
    writer.beginObject();
    writer.name("status").value(status);
    writer.name("rcode").value(rcode);
    writer.name("server").value(server);
    writer.name("qsize").value(qsize);
    writer.name("asize").value(asize);
    writer.name("flag_AA").value(flag_AA);
    writer.name("flag_TC").value(flag_TC);
    writer.name("flag_RD").value(flag_RD);
    writer.name("flag_RA").value(flag_RA);
    writer.name("flag_AD").value(flag_AD);
    writer.name("flag_CD").value(flag_CD);
    writer.name("answerText").value(answerText);
    writer.name("runtimestamp").value(runtimestamp);
    writer.name("duration").value(duration);
    writer.endObject();
}
 
Example 4
Source File: Session.java    From androdns with Apache License 2.0 6 votes vote down vote up
public void toJSON(JsonWriter writer) throws IOException {
    writer.beginObject();
    writer.name("qname").value(qname);
    writer.name("server").value(server);
    writer.name("qtype").value(qtype);
    writer.name("qclass").value(qclass);
    writer.name("protocol").value(protocol);
    writer.name("flag_rd").value(flag_RD);
    writer.name("flag_cd").value(flag_CD);
    writer.name("flag_do").value(flag_DO);
    writer.name("tcp").value(TCP);
    writer.name("port").value(port);


    writer.name("answer");
    if(answer != null){
        answer.toJSON(writer);
    } else {
        writer.nullValue();
    }

    writer.endObject();
}
 
Example 5
Source File: JsonUtils.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
public static String listToJsonStr(final List<Object> list) {
    if (list == null || list.isEmpty()) {
        return EMPTY_STRING;
    }
    final StringWriter sw = new StringWriter();
    final JsonWriter writer = new JsonWriter(sw);
    try {
        writer.beginArray();
        for (final Object o : list) {
            writer.beginObject();
            if (o instanceof Integer) {
                writer.name(INTEGER_CLASS_NAME).value((Integer)o);
            } else if (o instanceof String) {
                writer.name(STRING_CLASS_NAME).value((String)o);
            }
            writer.endObject();
        }
        writer.endArray();
        return sw.toString();
    } catch (final IOException e) {
    } finally {
        close(writer);
    }
    return EMPTY_STRING;
}
 
Example 6
Source File: GeneralPreferenceFragment.java    From NClientV2 with Apache License 2.0 6 votes vote down vote up
private String getDataSettings(Context context)throws IOException{
    String[]names=new String[]{"Settings","ScrapedTags"};
    StringWriter sw=new StringWriter();
    JsonWriter writer=new JsonWriter(sw);
    writer.setIndent("\t");

    writer.beginObject();
    for(String name:names)
        processSharedFromName(writer,context,name);
    writer.endObject();

    writer.flush();
    String settings=sw.toString();
    writer.close();

    LogUtility.d(settings);
    return settings;
}
 
Example 7
Source File: TagsAdapter.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
private static void writeTag(JsonWriter jw, Tag tag) throws IOException{
    jw.beginObject();
    jw.name("id").value(tag.getId());
    jw.name("name").value(tag.getName());
    jw.name("type").value(tag.getTypeSingleName());
    jw.endObject();
}
 
Example 8
Source File: GeneralPreferenceFragment.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
private void processSharedFromName(JsonWriter writer, Context context, String name)throws IOException{
    writer.name(name);
    writer.beginObject();
    SharedPreferences preferences=context.getSharedPreferences(name,0);
    for(Map.Entry<String,?> entry: preferences.getAll().entrySet()){
        writeEntry(writer,entry);
    }
    writer.endObject();
}
 
Example 9
Source File: AndroidPaymentApp.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static void serializeModifier(PaymentDetailsModifier modifier, JsonWriter json)
        throws IOException {
    // {{{
    json.beginObject();

    // total {{{
    if (modifier.total != null) {
        json.name("total");
        serializePaymentItem(modifier.total, json);
    } else {
        json.name("total").nullValue();
    }
    // }}} total

    // supportedMethods {{{
    json.name("supportedMethods").beginArray();
    for (String method : modifier.methodData.supportedMethods) {
        json.value(method);
    }
    json.endArray();
    // }}} supportedMethods

    // data {{{
    json.name("data").value(modifier.methodData.stringifiedData);
    // }}}

    json.endObject();
    // }}}
}
 
Example 10
Source File: AndroidPaymentApp.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static String serializeDetails(
        PaymentItem total, @Nullable List<PaymentItem> displayItems) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter json = new JsonWriter(stringWriter);
    try {
        // details {{{
        json.beginObject();

        // total {{{
        json.name("total");
        serializePaymentItem(total, json);
        // }}} total

        // displayitems {{{
        if (displayItems != null) {
            json.name("displayItems").beginArray();
            for (int i = 0; i < displayItems.size(); i++) {
                serializePaymentItem(displayItems.get(i), json);
            }
            json.endArray();
        }
        // }}} displayItems

        json.endObject();
        // }}} details
    } catch (IOException e) {
        return null;
    }

    return stringWriter.toString();
}
 
Example 11
Source File: Tag.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
void writeJson(JsonWriter writer)throws IOException{
    writer.beginObject();
    writer.name("count").value(count);
    writer.name("type").value(getTypeSingleName());
    writer.name("id").value(id);
    writer.name("name").value(name);
    writer.endObject();
}
 
Example 12
Source File: AndroidSmartyStreetsApiJsonParser.java    From Smarty-Streets-AutoCompleteTextView with Apache License 2.0 5 votes vote down vote up
void writePlace(JsonWriter writer, Address address) throws IOException {
    writer.beginObject();
    writer.name("text").value(address.getText());
    writer.name("street_line").value(address.getStreetLine());
    writer.name("city").value(address.getCity());
    writer.name("state").value(address.getState());
    writer.endObject();
}
 
Example 13
Source File: ShareManager.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
private static void encodeTuple(JsonWriter writer, ShareTuple tuple) throws IOException {
  if (tuple == ShareTuple.EMPTY_TUPLE) {
    writer.nullValue();
  } else {
    writer.beginObject();
    writer.name(WORKGROUP_KEY).value(tuple.mWorkgroup);
    writer.name(USERNAME_KEY).value(tuple.mUsername);
    writer.name(PASSWORD_KEY).value(tuple.mPassword);
    writer.name(MOUNT_KEY).value(tuple.mIsMounted);
    writer.endObject();
  }
}
 
Example 14
Source File: SimpleTest.java    From unmock-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonWriter() throws Exception {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw);
    jw.beginObject();
    jw.name("test");
    jw.value("world");
    jw.endObject();
    jw.flush();

    assertEquals("{\"test\":\"world\"}", sw.toString());

}
 
Example 15
Source File: SimpleTest.java    From unmock-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonWriter() throws Exception {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw);
    jw.beginObject();
    jw.name("test");
    jw.value("world");
    jw.endObject();
    jw.flush();

    assertEquals("{\"test\":\"world\"}", sw.toString());

}
 
Example 16
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void writePlace(JsonWriter writer, Place place) throws IOException {
    writer.beginObject();
    writer.name("description").value(place.description);
    writer.name("place_id").value(place.place_id);
    writer.name("matched_substrings");
    writeMatchedSubstringsArray(writer, place.matched_substrings);
    writer.name("terms");
    writeDescriptionTermsArray(writer, place.terms);
    writer.name("types");
    writePlaceTypesArray(writer, place.types);
    writer.endObject();
}
 
Example 17
Source File: AutofillPaymentInstrument.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
public void onFullCardDetails(CreditCard card, String cvc) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter json = new JsonWriter(stringWriter);
    try {
        json.beginObject();

        json.name("cardholderName").value(card.getName());
        json.name("cardNumber").value(card.getNumber());
        json.name("expiryMonth").value(card.getMonth());
        json.name("expiryYear").value(card.getYear());
        json.name("cardSecurityCode").value(cvc);

        if (mBillingAddress != null) {
            json.name("billingAddress").beginObject();

            json.name("country").value(ensureNotNull(mBillingAddress.getCountryCode()));
            json.name("region").value(ensureNotNull(mBillingAddress.getRegion()));
            json.name("city").value(ensureNotNull(mBillingAddress.getLocality()));
            json.name("dependentLocality")
                    .value(ensureNotNull(mBillingAddress.getDependentLocality()));

            json.name("addressLine").beginArray();
            String multipleLines = ensureNotNull(mBillingAddress.getStreetAddress());
            if (!TextUtils.isEmpty(multipleLines)) {
                String[] lines = multipleLines.split("\n");
                for (int i = 0; i < lines.length; i++) {
                    json.value(lines[i]);
                }
            }
            json.endArray();

            json.name("postalCode").value(ensureNotNull(mBillingAddress.getPostalCode()));
            json.name("sortingCode").value(ensureNotNull(mBillingAddress.getSortingCode()));
            json.name("languageCode").value(ensureNotNull(mBillingAddress.getLanguageCode()));
            json.name("organization").value(ensureNotNull(mBillingAddress.getCompanyName()));
            json.name("recipient").value(ensureNotNull(mBillingAddress.getFullName()));
            json.name("careOf").value("");
            json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumber()));

            json.endObject();
        }

        json.endObject();
    } catch (IOException e) {
        mCallback.onInstrumentDetailsError();
        return;
    }

    mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), stringWriter.toString());
}
 
Example 18
Source File: SnapshotBuilder.java    From clevertap-android-sdk with MIT License 4 votes vote down vote up
private static void writeViewProperties(JsonWriter j, View v, ViewSnapshotConfig snapshotConfig) throws IOException {
    final Class<?> viewClass = v.getClass();
    for (final ViewProperty desc : snapshotConfig.propertyDescriptionList) {
        if (desc.target.isAssignableFrom(viewClass) && null != desc.accessor) {
            final Object value = desc.accessor.invokeMethod(v);
            //noinspection StatementWithEmptyBody
            if (null == value) {
                // no-op
            }  else if (value instanceof Boolean) {
                j.name(desc.name).value((Boolean) value);
            } else if (value instanceof Number) {
                j.name(desc.name).value((Number) value);
            } else if (value instanceof ColorStateList) {
                j.name(desc.name).value((Integer) ((ColorStateList) value).getDefaultColor());
            } else if (value instanceof Drawable) {
                final Drawable drawable = (Drawable) value;
                final Rect bounds = drawable.getBounds();
                j.name(desc.name);
                j.beginObject();
                j.name("classes");
                j.beginArray();
                Class klass = drawable.getClass();
                while (klass != Object.class) {
                    if (klass != null) {
                        j.value(klass.getCanonicalName());
                        klass = klass.getSuperclass();
                    }
                }
                j.endArray();
                j.name("dimensions");
                j.beginObject();
                j.name("left").value(bounds.left);
                j.name("right").value(bounds.right);
                j.name("top").value(bounds.top);
                j.name("bottom").value(bounds.bottom);
                j.endObject();
                if (drawable instanceof ColorDrawable) {
                    final ColorDrawable colorDrawable = (ColorDrawable) drawable;
                    j.name("color").value(colorDrawable.getColor());
                }
                j.endObject();
            } else {
                j.name(desc.name).value(value.toString());
            }
        }
    }
}
 
Example 19
Source File: ViewSnapshot.java    From sa-sdk-android with Apache License 2.0 4 votes vote down vote up
private void addProperties(JsonWriter j, View v)
        throws IOException {
    final Class<?> viewClass = v.getClass();
    for (final PropertyDescription desc : mProperties) {
        if (desc.targetClass.isAssignableFrom(viewClass) && null != desc.accessor) {
            final Object value = desc.accessor.applyMethod(v);
            if (null == value) {
                // Don't produce anything in this case
            } else if (value instanceof Number) {
                j.name(desc.name).value((Number) value);
            } else if (value instanceof Boolean) {
                boolean clickable = (boolean) value;
                if (TextUtils.equals("clickable", desc.name)) {
                    if (VisualUtil.isSupportClick(v)) {
                        clickable = true;
                    } else if (VisualUtil.isForbiddenClick(v)) {
                        clickable = false;
                    }
                }
                j.name(desc.name).value(clickable);
            } else if (value instanceof ColorStateList) {
                j.name(desc.name).value((Integer) ((ColorStateList) value).getDefaultColor());
            } else if (value instanceof Drawable) {
                final Drawable drawable = (Drawable) value;
                final Rect bounds = drawable.getBounds();
                j.name(desc.name);
                j.beginObject();
                j.name("classes");
                j.beginArray();
                Class klass = drawable.getClass();
                while (klass != Object.class) {
                    j.value(klass.getCanonicalName());
                    klass = klass.getSuperclass();
                }
                j.endArray();
                j.name("dimensions");
                j.beginObject();
                j.name("left").value(bounds.left);
                j.name("right").value(bounds.right);
                j.name("top").value(bounds.top);
                j.name("bottom").value(bounds.bottom);
                j.endObject();
                if (drawable instanceof ColorDrawable) {
                    final ColorDrawable colorDrawable = (ColorDrawable) drawable;
                    j.name("color").value(colorDrawable.getColor());
                }
                j.endObject();
            } else {
                j.name(desc.name).value(value.toString());
            }
        }
    }
}
 
Example 20
Source File: ViewSnapshot.java    From ans-android-sdk with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private void addProperties(JsonWriter j, View v) throws IOException {
    final Class<?> viewClass = v.getClass();
    boolean processable = true;
    for (final PropertyDescription desc : mProperties) {
        if (desc.targetClass.isAssignableFrom(viewClass) && null != desc.accessor) {
            final Object value = desc.accessor.applyMethod(v);

            if (processable) {
                if (!TextUtils.isEmpty(desc.name)) {
                    if ("clickable".equals(desc.name)) {
                        boolean isClickable = (Boolean) value;
                        if (!isClickable || v instanceof AbsListView || v instanceof AbsoluteLayout) {
                            processable = false;
                        }
                    } else if ("alpha".equals(desc.name)) {
                        float alpha = (Float) value;
                        // 透明度是0则不可见
                        if (alpha == 0) {
                            processable = false;
                        }
                    } else if ("hidden".equals(desc.name)) {
                        int hide = (Integer) value;
                        // hidden是0则隐藏
                        if (hide != 0) {
                            processable = false;
                        }
                    }
                }
            }
            j.name("processable").value(processable ? "1" : "0");
            if (null == value) {
                // Don't produce anything in this case
            } else if (value instanceof Number) {
                j.name(desc.name).value((Number) value);
            } else if (value instanceof Boolean) {
                j.name(desc.name).value((Boolean) value);
            } else if (value instanceof ColorStateList) {
                j.name(desc.name).value((Integer) ((ColorStateList) value).getDefaultColor());
            } else if (value instanceof Drawable) {
                final Drawable drawable = (Drawable) value;
                final Rect bounds = drawable.getBounds();
                j.name(desc.name);
                j.beginObject();
                j.name("classes");
                j.beginArray();
                Class<?> klass = drawable.getClass();
                while (klass != Object.class) {
                    j.value(klass.getCanonicalName());
                    klass = klass.getSuperclass();
                }
                j.endArray();
                j.name("dimensions");
                j.beginObject();
                j.name("left").value(bounds.left);
                j.name("right").value(bounds.right);
                j.name("top").value(bounds.top);
                j.name("bottom").value(bounds.bottom);
                j.endObject();
                if (drawable instanceof ColorDrawable) {
                    final ColorDrawable colorDrawable = (ColorDrawable) drawable;
                    j.name("color").value(colorDrawable.getColor());
                }
                j.endObject();
            } else {
                j.name(desc.name).value(value.toString());
            }
        }
    }
}