diff --git a/examples/src/main/java/com/squareup/moshi/recipes/ByteStrings.java b/examples/src/main/java/com/squareup/moshi/recipes/ByteStrings.java new file mode 100644 index 0000000..895d9fe --- /dev/null +++ b/examples/src/main/java/com/squareup/moshi/recipes/ByteStrings.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.moshi.recipes; + +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.JsonReader; +import com.squareup.moshi.JsonWriter; +import com.squareup.moshi.Moshi; +import java.io.IOException; +import okio.ByteString; + +public final class ByteStrings { + public void run() throws Exception { + String json = "\"TW9zaGksIE9saXZlLCBXaGl0ZSBDaGluPw\""; + + Moshi moshi = new Moshi.Builder() + .add(ByteString.class, new Base64ByteStringAdapter()) + .build(); + JsonAdapter jsonAdapter = moshi.adapter(ByteString.class); + + ByteString byteString = jsonAdapter.fromJson(json); + System.out.println(byteString); + } + + /** + * Formats byte strings using Base64. No line + * breaks or whitespace is included in the encoded form. + */ + public final class Base64ByteStringAdapter extends JsonAdapter { + @Override public ByteString fromJson(JsonReader reader) throws IOException { + String base64 = reader.nextString(); + return ByteString.decodeBase64(base64); + } + + @Override public void toJson(JsonWriter writer, ByteString value) throws IOException { + String string = value.base64(); + writer.value(string); + } + } + + public static void main(String[] args) throws Exception { + new ByteStrings().run(); + } +}