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 Project: openboard   Author: dslul   File: JsonUtils.java    License: 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 Project: NClientV2   Author: Dar9586   File: GeneralPreferenceFragment.java    License: 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 #3
Source Project: react-native-GPay   Author: hellochirag   File: JSDebuggerWebSocketClient.java    License: 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 #4
Source Project: react-native-GPay   Author: hellochirag   File: JSDebuggerWebSocketClient.java    License: 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 #5
Source Project: react-native-GPay   Author: hellochirag   File: JSDebuggerWebSocketClient.java    License: 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 #6
Source Project: cwac-saferoom   Author: commonsguy   File: TypeTransmogrifier.java    License: 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 #7
Source Project: AOSP-Kayboard-7.1.2   Author: sergchil   File: JsonUtils.java    License: 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 Project: androdns   Author: gryphius   File: Session.java    License: 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 Project: androdns   Author: gryphius   File: SessionStorage.java    License: 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 Project: androdns   Author: gryphius   File: AnswerScreenState.java    License: 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 Project: samba-documents-provider   Author: google   File: ShareManager.java    License: 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 #12
Source Project: Indic-Keyboard   Author: smc   File: JsonUtils.java    License: 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 #13
Source Project: NClientV2   Author: Dar9586   File: Tag.java    License: 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 #14
Source Project: NClientV2   Author: Dar9586   File: Gallery.java    License: 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();
}
 
Example #15
Source Project: NClientV2   Author: Dar9586   File: Gallery.java    License: 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 #16
Source Project: NClientV2   Author: Dar9586   File: Gallery.java    License: Apache License 2.0 5 votes vote down vote up
private void toJsonTitle(JsonWriter writer)throws IOException{
    String title;
    writer.name("title");
    writer.beginObject();
    if((title=getTitle(TitleType.JAPANESE))!=null)
        writer.name("japanese").value(title);
    if((title=getTitle(TitleType.PRETTY))!=null)
        writer.name("pretty").value(title);
    if((title=getTitle(TitleType.ENGLISH))!=null)
        writer.name("english").value(title);
    writer.endObject();
}
 
Example #17
Source Project: NClientV2   Author: Dar9586   File: TagsAdapter.java    License: 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 #18
Source Project: NClientV2   Author: Dar9586   File: GeneralPreferenceFragment.java    License: 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 #19
Source Project: NClientV2   Author: Dar9586   File: GeneralPreferenceFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void writeEntry(JsonWriter writer, Map.Entry<String,?> entry)throws IOException {
    writer.name(entry.getKey());
         if(entry.getValue() instanceof Integer) writer.value((Integer)entry.getValue());
    else if(entry.getValue() instanceof Boolean) writer.value((Boolean)entry.getValue());
    else if(entry.getValue() instanceof String ) writer.value((String) entry.getValue());
    else if(entry.getValue() instanceof Long   ) writer.value((Long)   entry.getValue());
}
 
Example #20
Source Project: firebase-android-sdk   Author: firebase   File: JsonValueObjectEncoderContext.java    License: Apache License 2.0 5 votes vote down vote up
JsonValueObjectEncoderContext(
    @NonNull Writer writer,
    @NonNull Map<Class<?>, ObjectEncoder<?>> objectEncoders,
    @NonNull Map<Class<?>, ValueEncoder<?>> valueEncoders,
    ObjectEncoder<Object> fallbackEncoder,
    boolean ignoreNullValues) {
  this.jsonWriter = new JsonWriter(writer);
  this.objectEncoders = objectEncoders;
  this.valueEncoders = valueEncoders;
  this.fallbackEncoder = fallbackEncoder;
  this.ignoreNullValues = ignoreNullValues;
}
 
Example #21
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 #22
@Before
public void setUp() throws IOException {
    parser = new AndroidSmartyStreetsApiJsonParser();
    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 #23
Source Project: samba-documents-provider   Author: google   File: ShareManager.java    License: 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 #24
Source Project: unmock-plugin   Author: bjoernQ   File: SimpleTest.java    License: 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 #25
Source Project: unmock-plugin   Author: bjoernQ   File: SimpleTest.java    License: 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 #26
Source Project: sa-sdk-android   Author: sensorsdata   File: ViewSnapshot.java    License: 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 #27
Source Project: 365browser   Author: mogoweb   File: AndroidPaymentApp.java    License: 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 #28
Source Project: 365browser   Author: mogoweb   File: AndroidPaymentApp.java    License: 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 #29
Source Project: 365browser   Author: mogoweb   File: AndroidPaymentApp.java    License: 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 #30
Source Project: 365browser   Author: mogoweb   File: AndroidPaymentApp.java    License: 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();
}