@@ -660,3 +660,73 @@ func (_ *testDecodeSuite) TestJsonNull(c *C) {
660660c .Assert (err , IsNil )
661661c .Assert (rows .Rows [0 ][3 ], HasLen , 0 )
662662}
663+
664+ func (_ * testDecodeSuite ) TestJsonCompatibility (c * C ) {
665+ // Table:
666+ // mysql> desc t11;
667+ // +----------+--------------+------+-----+---------+-------------------+
668+ // | Field | Type | Null | Key | Default | Extra |
669+ // +----------+--------------+------+-----+---------+-------------------+
670+ // | id | int(11) | YES | | NULL | |
671+ // | cfg | varchar(100) | YES | | NULL | |
672+ // | cfg_json | json | YES | | NULL | VIRTUAL GENERATED |
673+ // | age | int(11) | YES | | NULL | |
674+ // +----------+--------------+------+-----+---------+-------------------+
675+ // mysql> insert into t11(id, cfg) values (1, '{}');
676+
677+ // test json deserialization
678+ // mysql> update t11 set cfg = '{"a":1234}' where id = 1;
679+ // mysql> update test set cfg = '{}' where id = 1;
680+
681+ tableMapEventData := []byte ("l\x00 \x00 \x00 \x00 \x00 \x01 \x00 \x04 test\x00 \x03 t11\x00 \x04 \x03 \x0f \xf5 \x03 \x03 d\x00 \x04 \x0f " )
682+
683+ tableMapEvent := new (TableMapEvent )
684+ tableMapEvent .tableIDSize = 6
685+ err := tableMapEvent .Decode (tableMapEventData )
686+ c .Assert (err , IsNil )
687+
688+ rows := new (RowsEvent )
689+ rows .tableIDSize = 6
690+ rows .tables = make (map [uint64 ]* TableMapEvent )
691+ rows .tables [tableMapEvent .TableID ] = tableMapEvent
692+ rows .Version = 2
693+
694+ data := []byte ("l\x00 \x00 \x00 \x00 \x00 \x01 \x00 \x02 \x00 \x04 \xff \xf8 \x01 \x00 \x00 \x00 \x02 {}\x05 \x00 \x00 \x00 \x00 \x00 \x00 \x04 \x00 " )
695+ rows .Rows = nil
696+ err = rows .Decode (data )
697+ c .Assert (err , IsNil )
698+ c .Assert (rows .Rows [0 ][2 ], DeepEquals , []uint8 ("{}" ))
699+
700+ // after MySQL 5.7.22
701+ data = []byte ("l\x00 \x00 \x00 \x00 \x00 \x01 \x00 \x02 \x00 \x04 \xff \xff \xf8 \x01 \x00 \x00 \x00 \x02 {}\x05 \x00 \x00 \x00 \x00 \x00 \x00 \x04 \x00 \xf8 \x01 \x00 \x00 \x00 \n {\" a\" :1234}\r \x00 \x00 \x00 \x00 \x01 \x00 \x0c \x00 \x0b \x00 \x01 \x00 \x05 \xd2 \x04 a" )
702+ rows .Rows = nil
703+ err = rows .Decode (data )
704+ c .Assert (err , IsNil )
705+ c .Assert (rows .Rows [1 ][2 ], DeepEquals , []uint8 ("{}" ))
706+ c .Assert (rows .Rows [2 ][2 ], DeepEquals , []uint8 ("{\" a\" :1234}" ))
707+
708+ data = []byte ("l\x00 \x00 \x00 \x00 \x00 \x01 \x00 \x02 \x00 \x04 \xff \xff \xf8 \x01 \x00 \x00 \x00 \n {\" a\" :1234}\r \x00 \x00 \x00 \x00 \x01 \x00 \x0c \x00 \x0b \x00 \x01 \x00 \x05 \xd2 \x04 a\xf8 \x01 \x00 \x00 \x00 \x02 {}\x05 \x00 \x00 \x00 \x00 \x00 \x00 \x04 \x00 " )
709+ rows .Rows = nil
710+ err = rows .Decode (data )
711+ c .Assert (err , IsNil )
712+ c .Assert (rows .Rows [1 ][2 ], DeepEquals , []uint8 ("{\" a\" :1234}" ))
713+ c .Assert (rows .Rows [2 ][2 ], DeepEquals , []uint8 ("{}" ))
714+
715+ // before MySQL 5.7.22
716+ rows .ignoreJSONDecodeErr = true
717+ data = []byte ("l\x00 \x00 \x00 \x00 \x00 \x01 \x00 \x02 \x00 \x04 \xff \xff \xf8 \x01 \x00 \x00 \x00 \x02 {}\x05 \x00 \x00 \x00 \x00 \x01 \x00 \x0c \x00 \xf8 \x01 \x00 \x00 \x00 \n {\" a\" :1234}\r \x00 \x00 \x00 \x00 \x01 \x00 \x0c \x00 \x0b \x00 \x01 \x00 \x05 \xd2 \x04 a" )
718+ rows .Rows = nil
719+ err = rows .Decode (data )
720+ c .Assert (err , IsNil )
721+ c .Assert (rows .Rows [1 ][2 ], DeepEquals , []uint8 ("null" ))
722+ c .Assert (rows .Rows [2 ][2 ], DeepEquals , []uint8 ("{\" a\" :1234}" ))
723+
724+ rows .ignoreJSONDecodeErr = false
725+ data = []byte ("l\x00 \x00 \x00 \x00 \x00 \x01 \x00 \x02 \x00 \x04 \xff \xff \xf8 \x01 \x00 \x00 \x00 \n {\" a\" :1234}\r \x00 \x00 \x00 \x00 \x00 \x00 \x04 \x00 \x00 \x00 \x01 \x00 \x05 \xd2 \x04 a\xf8 \x01 \x00 \x00 \x00 \x02 {}\x05 \x00 \x00 \x00 \x00 \x00 \x00 \x04 \x00 " )
726+ rows .Rows = nil
727+ err = rows .Decode (data )
728+ c .Assert (err , IsNil )
729+ // this value is wrong in binlog, but can be parsed without error
730+ c .Assert (rows .Rows [1 ][2 ], DeepEquals , []uint8 ("{}" ))
731+ c .Assert (rows .Rows [2 ][2 ], DeepEquals , []uint8 ("{}" ))
732+ }
0 commit comments