private < L , R > TypeAdapter < Xor < L , R > > xorAdapter ( TypeAdapter < L > leftAdapter , TypeAdapter < R > rightAdapter ) { return new TypeAdapter < Xor < L , R > > ( ) { @ Override public Xor < L , R > read ( JsonReader in ) throws IOException { JsonElement json = TypeAdapters . JSON_ELEMENT . read ( in ) ; try { R value = rightAdapter . fromJsonTree ( json ) ; return Xor . right ( value ) ; } catch ( Throwable e ) { L value = leftAdapter . fromJsonTree ( json ) ; return Xor . left ( value ) ; } } @ Override public void write ( JsonWriter out , Xor < L , R > xor ) throws IOException { throw new JsonParseException ( "Writing Xor is not supported" ) ; } } ; }
Creates a generic Xor adapter by combining two other adapters - one for each side of the Xor type . It will first try to parse incoming JSON data as the right type and if that does not succeed it will try again with the left type .