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

The following examples show how to use android.util.JsonWriter#endArray() . 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 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 2
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 3
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@TypeConverter
public static String fromStringSet(Set<String> strings) {
  if (strings==null) {
    return(null);
  }

  StringWriter result=new StringWriter();
  JsonWriter json=new JsonWriter(result);

  try {
    json.beginArray();

    for (String s : strings) {
      json.value(s);
    }

    json.endArray();
    json.close();
  }
  catch (IOException e) {
    Log.e(TAG, "Exception creating JSON", e);
  }

  return(result.toString());
}
 
Example 4
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 5
Source File: SessionStorage.java    From androdns with Apache License 2.0 6 votes vote down vote up
public static void save(Context context, String filename, ArrayList<Session> sessions) {
    try {

        FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);

        JsonWriter writer = new JsonWriter(new OutputStreamWriter(fos, "UTF-8"));
        writer.setIndent("  ");
        writer.beginArray();
        for (Session s : sessions) {
            s.toJSON(writer);
        }
        writer.endArray();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: Gallery.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
private void toJsonTags(JsonWriter writer)throws IOException{
    writer.name("tags");
    writer.beginArray();
    for(Tag t:getTags().getAllTagsSet())
        t.writeJson(writer);
    writer.endArray();
}
 
Example 7
Source File: ViewSnapshot.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
private void snapshotViewHierarchy(JsonWriter j, View rootView)
        throws IOException {
    reset();
    j.beginArray();
    snapshotView(j, rootView, 0);
    j.endArray();
}
 
Example 8
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void writeDescriptionTermsArray(JsonWriter writer, List<DescriptionTerm> descriptionTerms) throws IOException {
    writer.beginArray();
    for (DescriptionTerm term : descriptionTerms) {
        writer.beginObject();
        writer.name("offset").value(term.offset);
        writer.name("value").value(term.value);
        writer.endObject();
    }
    writer.endArray();
}
 
Example 9
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void writeMatchedSubstringsArray(JsonWriter writer, List<MatchedSubstring> matchedSubstrings) throws IOException {
    writer.beginArray();
    for (MatchedSubstring matchedSubstring : matchedSubstrings) {
        writer.beginObject();
        writer.name("length").value(matchedSubstring.length);
        writer.name("offset").value(matchedSubstring.offset);
        writer.endObject();
    }
    writer.endArray();
}
 
Example 10
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 11
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 12
Source File: AndroidPaymentApp.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static String serializeModifiers(Collection<PaymentDetailsModifier> modifiers) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter json = new JsonWriter(stringWriter);
    try {
        json.beginArray();
        for (PaymentDetailsModifier modifier : modifiers) {
            serializeModifier(modifier, json);
        }
        json.endArray();
    } catch (IOException e) {
        return EMPTY_JSON_DATA;
    }
    return stringWriter.toString();
}
 
Example 13
Source File: SnapshotBuilder.java    From clevertap-android-sdk with MIT License 4 votes vote down vote up
private static void viewHierarchySnapshot(JsonWriter j, View rootView, ViewSnapshotConfig snapshotConfig) throws IOException {
    j.beginArray();
    viewSnapshot(j, rootView, snapshotConfig);
    j.endArray();
}
 
Example 14
Source File: ViewSnapshot.java    From ans-android-sdk with GNU General Public License v3.0 4 votes vote down vote up
private void snapshotViewHierarchy(JsonWriter j, View rootView) throws IOException {
    j.beginArray();
    snapshotView(j, rootView);
    j.endArray();
}
 
Example 15
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 16
Source File: AutofillPaymentInstrument.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Stringify the card details and send the resulting string and the method name to the
 * registered callback.
 */
private void sendInstrumentDetails() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter json = new JsonWriter(stringWriter);
    try {
        json.beginObject();

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

        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("phone").value(ensureNotNull(mBillingAddress.getPhoneNumber()));

        json.endObject();

        json.endObject();
    } catch (IOException e) {
        onFullCardError();
        return;
    } finally {
        mSecurityCode = "";
    }

    mCallback.onInstrumentDetailsReady(mMethodName, stringWriter.toString());
}
 
Example 17
Source File: ViewSnapshot.java    From sa-sdk-android with Apache License 2.0 4 votes vote down vote up
private void mergeWebViewNodes(JsonWriter j, WebNode view, View webView, float webViewScale) {
    try {
        j.beginObject();
        j.name("hashCode").value(view.getId());
        j.name("index").value(0);
        if (!TextUtils.isEmpty(view.get$element_selector())) {
            j.name("element_selector").value(view.get$element_selector());
        }
        if (!TextUtils.isEmpty(view.get$element_content())) {
            j.name("element_content").value(view.get$element_content());
        }
        j.name("element_level").value(++mSnapInfo.elementLevel);
        j.name("h5_title").value(view.get$title());
        float scale = view.getScale();
        if (webViewScale == 0) {
            webViewScale = scale;
        }
        // 原生 WebView getScrollX 能取到值,而 X5WebView 始终是 0
        float left = 0f, top = 0f;
        if (webView.getScrollX() == 0) {
            left = view.getLeft() * webViewScale;
        } else {
            left = (view.getLeft() + view.getScrollX()) * webViewScale;
        }
        if (webView.getScrollY() == 0) {
            top = view.getTop() * webViewScale;
        } else {
            top = (view.getTop() + view.getScrollY()) * webViewScale;
        }
        j.name("left").value((int) left);
        j.name("top").value((int) top);
        j.name("width").value((int) (view.getWidth() * webViewScale));
        j.name("height").value((int) (view.getHeight() * webViewScale));
        j.name("scrollX").value(0);
        j.name("scrollY").value(0);
        j.name("visibility").value(view.isVisibility() ? View.VISIBLE : View.GONE);
        j.name("url").value(view.get$url());
        j.name("clickable").value(true);
        j.name("importantForAccessibility").value(true);
        j.name("is_h5").value(true);

        j.name("classes");
        j.beginArray();
        j.value(view.getTagName());
        Class<?> klass = webView.getClass();
        do {
            j.value(klass.getCanonicalName());
            klass = klass.getSuperclass();
        } while (klass != Object.class && klass != null);
        j.endArray();

        List<String> list = view.getSubelements();
        if (list != null && list.size() > 0) {
            j.name("subviews");
            j.beginArray();
            for (String id : list) {
                j.value(id);
            }
            j.endArray();
        }
        j.endObject();
    } catch (IOException e) {
        SALog.printStackTrace(e);
    }

}
 
Example 18
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 19
Source File: AutofillPaymentInstrument.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Stringify the card details and send the resulting string and the method name to the
 * registered callback.
 */
private void sendIntrumentDetails() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter json = new JsonWriter(stringWriter);
    try {
        json.beginObject();

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

        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("phone").value(ensureNotNull(mBillingAddress.getPhoneNumber()));

        json.endObject();

        json.endObject();
    } catch (IOException e) {
        onFullCardError();
        return;
    } finally {
        mSecurityCode = "";
    }

    mCallback.onInstrumentDetailsReady(
            mCard.getBasicCardPaymentType(), stringWriter.toString());
}
 
Example 20
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());
}