下面列出了android.util.JsonReader#nextBoolean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
OpenHours readOpeningHours(JsonReader reader) throws IOException {
boolean openNow = false;
List<OpenPeriod> periods = null;
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "open_now":
openNow = reader.nextBoolean();
break;
case "periods":
periods = readOpenPeriodsArray(reader);
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
return new OpenHours(openNow, periods);
}
private static ShareTuple decodeTuple(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return ShareTuple.EMPTY_TUPLE;
}
String workgroup = null;
String username = null;
String password = null;
boolean mounted = true;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case WORKGROUP_KEY:
workgroup = reader.nextString();
break;
case USERNAME_KEY:
username = reader.nextString();
break;
case PASSWORD_KEY:
password = reader.nextString();
break;
case MOUNT_KEY:
mounted = reader.nextBoolean();
default:
Log.w(TAG, "Ignoring unknown key " + name);
}
}
reader.endObject();
return new ShareTuple(workgroup, username, password, mounted);
}
@NonNull
private static StickerPack readStickerPack(@NonNull JsonReader reader) throws IOException, IllegalStateException {
reader.beginObject();
String identifier = null;
String name = null;
String publisher = null;
String trayImageFile = null;
String publisherEmail = null;
String publisherWebsite = null;
String privacyPolicyWebsite = null;
String licenseAgreementWebsite = null;
String imageDataVersion = "";
boolean avoidCache = false;
List<Sticker> stickerList = null;
while (reader.hasNext()) {
String key = reader.nextName();
switch (key) {
case "identifier":
identifier = reader.nextString();
break;
case "name":
name = reader.nextString();
break;
case "publisher":
publisher = reader.nextString();
break;
case "tray_image_file":
trayImageFile = reader.nextString();
break;
case "publisher_email":
publisherEmail = reader.nextString();
break;
case "publisher_website":
publisherWebsite = reader.nextString();
break;
case "privacy_policy_website":
privacyPolicyWebsite = reader.nextString();
break;
case "license_agreement_website":
licenseAgreementWebsite = reader.nextString();
break;
case "stickers":
stickerList = readStickers(reader);
break;
case "image_data_version":
imageDataVersion = reader.nextString();
break;
case "avoid_cache":
avoidCache = reader.nextBoolean();
break;
default:
reader.skipValue();
}
}
if (TextUtils.isEmpty(identifier)) {
throw new IllegalStateException("identifier cannot be empty");
}
if (TextUtils.isEmpty(name)) {
throw new IllegalStateException("name cannot be empty");
}
if (TextUtils.isEmpty(publisher)) {
throw new IllegalStateException("publisher cannot be empty");
}
if (TextUtils.isEmpty(trayImageFile)) {
throw new IllegalStateException("tray_image_file cannot be empty");
}
if (stickerList == null || stickerList.size() == 0) {
throw new IllegalStateException("sticker list is empty");
}
if (identifier.contains("..") || identifier.contains("/")) {
throw new IllegalStateException("identifier should not contain .. or / to prevent directory traversal");
}
if (TextUtils.isEmpty(imageDataVersion)) {
throw new IllegalStateException("image_data_version should not be empty");
}
reader.endObject();
final StickerPack stickerPack = new StickerPack(identifier, name, publisher, trayImageFile, publisherEmail, publisherWebsite, privacyPolicyWebsite, licenseAgreementWebsite, imageDataVersion, avoidCache);
stickerPack.setStickers(stickerList);
return stickerPack;
}
private static ZCashTransactionDetails_taddr readTx(JsonReader reader) throws IOException, IllegalStateException {
ZCashTransactionDetails_taddr tx = new ZCashTransactionDetails_taddr();
if(reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new IOException("Cannot parse JSON");
}
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case HASH:
tx.hash = reader.nextString();
break;
case MAINCHAIN:
tx.mainChain = reader.nextBoolean();
break;
case FEE:
tx.fee = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case TYPE:
tx.type = reader.nextString();
break;
case SHIELDED:
tx.shielded = reader.nextBoolean();
break;
case INDEX:
tx.index = reader.nextLong();
break;
case BLOCKHASH:
tx.blockHash = reader.nextString();
break;
case BLOCKHEIGHT:
tx.blockHeight = reader.nextLong();
break;
case VERSION:
tx.version = reader.nextLong();
break;
case LOCKTIME:
tx.locktime = reader.nextLong();
break;
case TIME:
tx.time = reader.nextLong();
break;
case TIMESTAMP:
tx.timestamp = reader.nextLong();
break;
case VIN:
tx.vin = readTxInputs(reader);
break;
case VOUT:
tx.vout = readTxOutputs(reader, null);
break;
case VJOINSPLIT:
skipJoinSplits(reader);
break;
case VALUE:
tx.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case OUTPUTVALUE:
tx.outputValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case SHIELDEDVALUE:
tx.shieldedValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
default:
reader.skipValue();
}
}
reader.endObject();
return tx;
}
private static boolean readFieldBoolean(JsonReader reader) throws IOException {
reader.nextName();
return reader.nextBoolean();
}
private static ZCashTransactionDetails_taddr readTx(JsonReader reader) throws IOException, IllegalStateException {
ZCashTransactionDetails_taddr tx = new ZCashTransactionDetails_taddr();
if(reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new IOException("Cannot parse JSON");
}
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case HASH:
tx.hash = reader.nextString();
break;
case MAINCHAIN:
tx.mainChain = reader.nextBoolean();
break;
case FEE:
tx.fee = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case TYPE:
tx.type = reader.nextString();
break;
case SHIELDED:
tx.shielded = reader.nextBoolean();
break;
case INDEX:
tx.index = reader.nextLong();
break;
case BLOCKHASH:
tx.blockHash = reader.nextString();
break;
case BLOCKHEIGHT:
tx.blockHeight = reader.nextLong();
break;
case VERSION:
tx.version = reader.nextLong();
break;
case LOCKTIME:
tx.locktime = reader.nextLong();
break;
case TIME:
tx.time = reader.nextLong();
break;
case TIMESTAMP:
tx.timestamp = reader.nextLong();
break;
case VIN:
tx.vin = readTxInputs(reader);
break;
case VOUT:
tx.vout = readTxOutputs(reader, null);
break;
case VJOINSPLIT:
skipJoinSplits(reader);
break;
case VALUE:
tx.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case OUTPUTVALUE:
tx.outputValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case SHIELDEDVALUE:
tx.shieldedValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
default:
reader.skipValue();
}
}
reader.endObject();
return tx;
}
private static boolean readFieldBoolean(JsonReader reader) throws IOException {
reader.nextName();
return reader.nextBoolean();
}
private Sample readOfflineEntry(JsonReader reader) throws IOException {
String sampleName = null;
String downloadUri = null;
String offlineUri = null;
String extension = null;
UUID drmUuid = null;
String drmLicenseUrl = null;
String[] drmKeyRequestProperties = null;
boolean preferExtensionDecoders = false;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "name":
sampleName = reader.nextString();
break;
case "download_uri":
downloadUri = reader.nextString();
break;
case "offline_uri":
offlineUri = reader.nextString();
break;
case "extension":
extension = reader.nextString();
break;
case "drm_scheme":
drmUuid = getDrmUuid(reader.nextString());
break;
case "drm_license_url":
drmLicenseUrl = reader.nextString();
break;
case "drm_key_request_properties":
ArrayList<String> drmKeyRequestPropertiesList = new ArrayList<>();
reader.beginObject();
while (reader.hasNext()) {
drmKeyRequestPropertiesList.add(reader.nextName());
drmKeyRequestPropertiesList.add(reader.nextString());
}
reader.endObject();
drmKeyRequestProperties = drmKeyRequestPropertiesList.toArray(new String[0]);
break;
case "prefer_extension_decoders":
preferExtensionDecoders = reader.nextBoolean();
break;
default:
throw new ParserException("Unsupported attribute name: " + name);
}
}
reader.endObject();
return new OfflineSample(sampleName,drmUuid,drmLicenseUrl,drmKeyRequestProperties,
preferExtensionDecoders,offlineUri,downloadUri);
}
private Sample readEntry(JsonReader reader, boolean insidePlaylist) throws IOException {
String sampleName = null;
String uri = null;
String extension = null;
UUID drmUuid = null;
String drmLicenseUrl = null;
String[] drmKeyRequestProperties = null;
boolean preferExtensionDecoders = false;
ArrayList<UriSample> playlistSamples = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "name":
sampleName = reader.nextString();
break;
case "uri":
uri = reader.nextString();
break;
case "extension":
extension = reader.nextString();
break;
case "drm_scheme":
Assertions.checkState(!insidePlaylist, "Invalid attribute on nested item: drm_scheme");
drmUuid = getDrmUuid(reader.nextString());
break;
case "drm_license_url":
Assertions.checkState(!insidePlaylist,
"Invalid attribute on nested item: drm_license_url");
drmLicenseUrl = reader.nextString();
break;
case "drm_key_request_properties":
Assertions.checkState(!insidePlaylist,
"Invalid attribute on nested item: drm_key_request_properties");
ArrayList<String> drmKeyRequestPropertiesList = new ArrayList<>();
reader.beginObject();
while (reader.hasNext()) {
drmKeyRequestPropertiesList.add(reader.nextName());
drmKeyRequestPropertiesList.add(reader.nextString());
}
reader.endObject();
drmKeyRequestProperties = drmKeyRequestPropertiesList.toArray(new String[0]);
break;
case "prefer_extension_decoders":
Assertions.checkState(!insidePlaylist,
"Invalid attribute on nested item: prefer_extension_decoders");
preferExtensionDecoders = reader.nextBoolean();
break;
case "playlist":
Assertions.checkState(!insidePlaylist, "Invalid nesting of playlists");
playlistSamples = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
playlistSamples.add((UriSample) readEntry(reader, true));
}
reader.endArray();
break;
default:
throw new ParserException("Unsupported attribute name: " + name);
}
}
reader.endObject();
if (playlistSamples != null) {
UriSample[] playlistSamplesArray = playlistSamples.toArray(
new UriSample[playlistSamples.size()]);
return new PlaylistSample(sampleName, drmUuid, drmLicenseUrl, drmKeyRequestProperties,
preferExtensionDecoders, playlistSamplesArray);
} else {
return new UriSample(sampleName, drmUuid, drmLicenseUrl, drmKeyRequestProperties,
preferExtensionDecoders, uri, extension);
}
}
public static LoginResult parseLoginResponseContent(final InputStream content) throws IOException {
//A failure by default.
LoginResult result = new LoginResult(false, null, null);
final InputStreamReader streamReader = new InputStreamReader(content, Utils.STRING_ENCODING);
final JsonReader reader = new JsonReader(streamReader);
reader.beginObject();
boolean success = false;
String apiKey = null;
String userName = null;
String message = null;
while (reader.hasNext()) {
final String name = reader.nextName();
switch (name) {
case "success":
success = reader.nextBoolean();
break;
case "api_key":
apiKey = reader.nextString();
break;
case "name":
userName = reader.nextString();
break;
case "message":
message = reader.nextString();
break;
default:
reader.skipValue();
}
}
if (success) {
result = new LoginResult(true, userName, apiKey);
} else {
Log.info("Login failed.");
Log.info("Login failure message: " + message);
}
reader.endObject();
reader.close();
streamReader.close();
return result;
}