THRIFT-2288 Go impl of Thrift JSON protocol wrongly writes/expects true/false for...
authorJens Geyer <jensg@apache.org>
Tue, 17 Dec 2013 20:39:30 +0000 (21:39 +0100)
committerJens Geyer <jensg@apache.org>
Tue, 17 Dec 2013 20:39:30 +0000 (21:39 +0100)
Patch: Jens Geyer

lib/go/thrift/json_protocol.go
lib/go/thrift/json_protocol_test.go

index 5a432c3..7a5e16e 100644 (file)
@@ -158,7 +158,12 @@ func (p *TJSONProtocol) WriteSetEnd() error {
 }
 
 func (p *TJSONProtocol) WriteBool(b bool) error {
-       return p.OutputBool(b)
+       //return p.OutputBool(b) - not for Thrift JSON
+       if b {
+               return p.WriteI32(1)
+    } else {
+               return p.WriteI32(0)
+    }
 }
 
 func (p *TJSONProtocol) WriteByte(b byte) error {
@@ -325,45 +330,8 @@ func (p *TJSONProtocol) ReadSetEnd() error {
 }
 
 func (p *TJSONProtocol) ReadBool() (bool, error) {
-       var value bool
-       if err := p.ParsePreValue(); err != nil {
-               return value, err
-       }
-       b, _ := p.reader.Peek(len(JSON_FALSE))
-       if len(b) > 0 {
-               switch b[0] {
-               case JSON_TRUE[0]:
-                       if string(b[0:len(JSON_TRUE)]) == string(JSON_TRUE) {
-                               p.reader.Read(b[0:len(JSON_TRUE)])
-                               value = true
-                       } else {
-                               e := fmt.Errorf("Expected \"true\" but found: %s", string(b))
-                               return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
-                       }
-                       break
-               case JSON_FALSE[0]:
-                       if string(b[0:len(JSON_FALSE)]) == string(JSON_FALSE) {
-                               p.reader.Read(b[0:len(JSON_FALSE)])
-                               value = false
-                       } else {
-                               e := fmt.Errorf("Expected \"false\" but found: %s", string(b))
-                               return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
-                       }
-                       break
-               case JSON_NULL[0]:
-                       if string(b[0:len(JSON_NULL)]) == string(JSON_NULL) {
-                               p.reader.Read(b[0:len(JSON_NULL)])
-                               value = false
-                       } else {
-                               e := fmt.Errorf("Expected \"null\" but found: %s", string(b))
-                               return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
-                       }
-               default:
-                       e := fmt.Errorf("Expected \"true\", \"false\", or \"null\" but found: %s", string(b))
-                       return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
-               }
-       }
-       return value, p.ParsePostValue()
+       value, err := p.ReadI32(); 
+       return (value != 0), err
 }
 
 func (p *TJSONProtocol) ReadByte() (byte, error) {
index cb626cc..8542a96 100644 (file)
@@ -40,11 +40,17 @@ func TestWriteJSONProtocolBool(t *testing.T) {
                        t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
                }
                s := trans.String()
-               if s != fmt.Sprint(value) {
-                       t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
+               expected := ""
+               if value {
+                       expected = "1"
+               } else {
+                       expected = "0"
                }
-               v := false
-               if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
+               if s != expected {
+                       t.Fatalf("Bad value for %s %v: %s expected", thetype, value, s)
+               }
+               v := -1
+               if err := json.Unmarshal([]byte(s), &v); err != nil || (v != 0) != value {
                        t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
                }
                trans.Reset()
@@ -58,9 +64,9 @@ func TestReadJSONProtocolBool(t *testing.T) {
                trans := NewTMemoryBuffer()
                p := NewTJSONProtocol(trans)
                if value {
-                       trans.Write(JSON_TRUE)
+                       trans.Write([]byte{'1'}) // not JSON_TRUE
                } else {
-                       trans.Write(JSON_FALSE)
+                       trans.Write([]byte{'0'}) // not JSON_FALSE
                }
                trans.Flush()
                s := trans.String()
@@ -71,8 +77,9 @@ func TestReadJSONProtocolBool(t *testing.T) {
                if v != value {
                        t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
                }
-               if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
-                       t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
+               vv := -1
+               if err := json.Unmarshal([]byte(s), &vv); err != nil || (vv != 0) != value {
+                       t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, vv)
                }
                trans.Reset()
                trans.Close()