2017-02-22 12 views
3

Gsonを使用してJSONを直列化および逆シリアル化しています。私のテストでは、Gsonオブジェクトに空の文字列を渡したときに、JsonParseExceptionを投げるのではなく、nullを返します。空の文字列をデシリアライズするときにGsonがnullを返さないようにする

空の文字列にエラーが発生するようにGsonを設定するにはどうすればよいですか?

public static Gson gson() { 

    final GsonBuilder gsonBuilder = new GsonBuilder(); 

    // etc... 

    gsonBuilder.registerTypeAdapter(Project.class, new ProjectSerializer()); 
    gsonBuilder.registerTypeAdapter(Project.class, new ProjectDeserializer()); 

    // etc... 

    return gsonBuilder.create(); 
} 

答えて

1

あなたは箱から出しているようにそれをすることはできません。

public final class ProjectDeserializer implements JsonDeserializer<Project> { 

    @Override 
    public Project deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException { 

     final JsonObject jsonObject = jsonElement.getAsJsonObject(); 

     final Identifier name = context.deserialize(jsonObject.get("name"), Identifier.class); 

     Optional<String> license; 
     if (jsonObject.has("license")) { 
      license = Optional.of(jsonObject.get("license").getAsString()); 
     } else { 
      license = Optional.empty(); 
     } 

     DependencyGroup dependencies; 
     if (jsonObject.has("dependencies")) { 
      dependencies = context.deserialize(jsonObject.get("dependencies"), DependencyGroup.class); 
     } else { 
      dependencies = DependencyGroup.of(); 
     } 

     return Project.of(name, license, dependencies); 
    } 
} 

はここに私のGson工場です:


は、ここに私のデシリアライザです。現在com.google.gson.Gsonは、コードを保護し、次のコードを持っている:メソッドに空または空の文字列を渡す

public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { 
    boolean isEmpty = true; 
    ... 
    try { 
     reader.peek(); 
     ... 
    } catch (EOFException e) { 
     /* 
     * For compatibility with JSON 1.5 and earlier, we return null for empty 
     * documents instead of throwing. 
     */ 
     if (isEmpty) { 
     return null; 
     } 
     throw new JsonSyntaxException(e); 

EOFExceptionをスローするreader.peek();行を行い、isEmptyフラグがその時点でtrueに設定されています。そしてこれがあなたのデシリアライザがそれに何の影響も与えない理由です:それは呼び出されていません。

後方互換性のため

、空の文字列にnullを返します。この動作は、メッセージの抜粋をコミット以下で25c6ae177b1ca56db7f3c29eb574bdd032a06165back in 2011をコミット中に導入されました。

何ができることは、あなたがnullのための直列化復元オブジェクトを確認するか、(集中直列化復元があなたのために可能であれば[少なくともそれがあるべき])Gson.fromJson(JsonReader,Type)は、デシリアライゼーションの目的のために使用されていることを確認していることを確認して次のいずれかです。このその方法で現在キャッチされていないJsonSyntaxExceptionにカスタムJsonReaderのラップEOFExceptionを挿入できます(少なくともGson 2.8.0以降)。例:

public class EmptyStringFailFastJsonReader 
     extends JsonReader { 

    public EmptyStringFailFastJsonReader(final Reader in) { 
     super(in); 
    } 

    @Override 
    public JsonToken peek() 
      throws IOException { 
     try { 
      return super.peek(); 
     } catch (final EOFException ex) { 
      throw new JsonSyntaxException(ex); 
     } 
    } 

} 

これはGsonの保護コードを破損します。以下にいくつかの例を示します。失敗しないコード:

com.google.gson.JsonSyntaxException:java.io次の例外メッセージが表示されて

gson.fromJson(new EmptyStringFailFastJsonReader(new StringReader("")), fooType); 

gson.fromJson("", fooType); 
gson.fromJson(new StringReader(""), fooType); 

しかし、一つにはない以下の.EOFException:1行目の1行目の入力の最後$

+0

詳細な回答ありがとうございます。私は私のプロジェクトのための最善の解決策は、私が逆シリアル化し、そこにチェックを行うかもしれない各タイプの静的メソッドを持っていると思う。 – sdgfsdh

+0

@sdgfsdhよろしくお願いします。 –

関連する問題