android.util.JsonWriter Java Examples

The following examples show how to use android.util.JsonWriter. 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: 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 #2
Source File: JSDebuggerWebSocketClient.java    From react-native-GPay with MIT License 6 votes vote down vote up
public void executeJSCall(
    String methodName,
    String jsonArgsArray,
    JSDebuggerCallback callback) {
  int requestID = mRequestID.getAndIncrement();
  mCallbacks.put(requestID, callback);

  try {
    StringWriter sw = new StringWriter();
    JsonWriter js = new JsonWriter(sw);

    js.beginObject()
      .name("id").value(requestID)
      .name("method").value(methodName);
    /* JsonWriter does not offer writing raw string (without quotes), that's why
       here we directly write to output string using the the underlying StringWriter */
    sw.append(",\"arguments\":").append(jsonArgsArray);
    js.endObject().close();
    sendMessage(requestID, sw.toString());
  } catch (IOException e) {
    triggerRequestFailure(requestID, e);
  }
}
 
Example #3
Source File: JSDebuggerWebSocketClient.java    From react-native-GPay with MIT License 6 votes vote down vote up
public void loadApplicationScript(
    String sourceURL,
    HashMap<String, String> injectedObjects,
    JSDebuggerCallback callback) {
  int requestID = mRequestID.getAndIncrement();
  mCallbacks.put(requestID, callback);

  try {
    StringWriter sw = new StringWriter();
    JsonWriter js = new JsonWriter(sw)
       .beginObject()
       .name("id").value(requestID)
       .name("method").value("executeApplicationScript")
       .name("url").value(sourceURL)
       .name("inject").beginObject();
    for (String key : injectedObjects.keySet()) {
      js.name(key).value(injectedObjects.get(key));
    }
    js.endObject().endObject().close();
    sendMessage(requestID, sw.toString());
  } catch (IOException e) {
    triggerRequestFailure(requestID, e);
  }
}
 
Example #4
Source File: JSDebuggerWebSocketClient.java    From react-native-GPay with MIT License 6 votes vote down vote up
public void prepareJSRuntime(JSDebuggerCallback callback) {
  int requestID = mRequestID.getAndIncrement();
  mCallbacks.put(requestID, callback);

  try {
    StringWriter sw = new StringWriter();
    JsonWriter js = new JsonWriter(sw);
    js.beginObject()
      .name("id").value(requestID)
      .name("method").value("prepareJSRuntime")
      .endObject()
      .close();
    sendMessage(requestID, sw.toString());
  } catch (IOException e) {
    triggerRequestFailure(requestID, e);
  }
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: ShareManager.java    From samba-documents-provider with GNU General Public License v3.0 6 votes vote down vote up
private static String encode(String uri, ShareTuple tuple) {
  final StringWriter stringWriter = new StringWriter();
  try (final JsonWriter jsonWriter = new JsonWriter(stringWriter)) {
    jsonWriter.beginObject();
    jsonWriter.name(URI_KEY).value(uri);

    jsonWriter.name(CREDENTIAL_TUPLE_KEY);
    encodeTuple(jsonWriter, tuple);
    jsonWriter.endObject();
  } catch (IOException e) {
    Log.e(TAG, "Failed to encode credential for " + uri);
    return null;
  }

  return stringWriter.toString();
}
 
Example #13
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void writePlaceTypesArray(JsonWriter writer, List<PlaceType> placeTypes) throws IOException {
    writer.beginArray();
    for (PlaceType type : placeTypes) {
        switch (type) {
            case ROUTE:
                writer.value("route");
                break;
            case GEOCODE:
                writer.value("geocode");
                break;
        }
    }
    writer.endArray();
}
 
Example #14
Source File: AndroidPlacesApiJsonWriterTest.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Before
public void setUp() throws IOException {
    parser = new AndroidPlacesApiJsonParser();
    writer = mock(JsonWriter.class);
    actual = new ArrayList<>();

    when(writer.beginArray()).then(new CompositeValueAnswer(BEGIN_ARRAY));
    when(writer.endArray()).then(new CompositeValueAnswer(END_ARRAY));
    when(writer.beginObject()).then(new CompositeValueAnswer(BEGIN_OBJECT));
    when(writer.endObject()).then(new CompositeValueAnswer(END_OBJECT));
    when(writer.value(anyInt())).then(new ValueAnswer());
    when(writer.value(anyBoolean())).then(new ValueAnswer());
    when(writer.value(anyString())).then(new ValueAnswer());
    when(writer.name(anyString())).then(new ValueAnswer());
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
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 #20
Source File: AndroidPaymentApp.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static String serializeTotalAmount(PaymentCurrencyAmount totalAmount) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter json = new JsonWriter(stringWriter);
    try {
        // {{{
        json.beginObject();
        json.name("currency").value(totalAmount.currency);
        json.name("value").value(totalAmount.value);
        json.endObject();
        // }}}
    } catch (IOException e) {
        return null;
    }
    return stringWriter.toString();
}
 
Example #21
Source File: AndroidPaymentApp.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static void serializePaymentItem(PaymentItem item, JsonWriter json) throws IOException {
    // item {{{
    json.beginObject();
    json.name("label").value(item.label);

    // amount {{{
    json.name("amount").beginObject();
    json.name("currency").value(item.amount.currency);
    json.name("value").value(item.amount.value);
    json.endObject();
    // }}} amount

    json.endObject();
    // }}} item
}
 
Example #22
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 #23
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 #24
Source File: BackupExporter.java    From NekoSMS with GNU General Public License v3.0 5 votes vote down vote up
public BackupExporter(OutputStream out) {
    OutputStreamWriter streamWriter;
    try {
        streamWriter = new OutputStreamWriter(out, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
    mJsonWriter = new JsonWriter(streamWriter);
    mJsonWriter.setIndent("\t");
}
 
Example #25
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 #26
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 #27
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 #28
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 #29
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 #30
Source File: Gallery.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public void jsonWrite(Writer ww)throws IOException{
    //images aren't saved
    JsonWriter writer=new JsonWriter(ww);
    writer.beginObject();
    writer.name("id").value(getId());
    writer.name("media_id").value(getMediaId());
    writer.name("upload_date").value(getUploadDate().getTime()/1000);
    writer.name("num_favorites").value(getFavoriteCount());
    toJsonTitle(writer);
    toJsonTags(writer);

    writer.endObject();
    writer.flush();
}