暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL json内容特殊字符使用unicode代替

digoal 2018-08-22
705

作者

digoal

日期

2018-08-22

标签

PostgreSQL , json , 特殊字符 , unicode


背景

json中包含特殊字符时,可能导致输入异常,可以转换为UNICODE后,即可正常输入。

格式为 \u[0-f]{4}

```
postgres=# select '{"id1":1, "id2":2, "info":"digoal d\u0061t\u0061 ab", "c1":123, "c2":1.1, "c3":1.9999, "crt_time":"2018-01-01 10:10:10"}'::jsonb;
jsonb


{"c1": 123, "c2": 1.1, "c3": 1.9999, "id1": 1, "id2": 2, "info": "digoal data ab", "crt_time": "2018-01-01 10:10:10"}
(1 row)
```

格式

\u[0-f]{4}

```
json_lex_string@src/backend/utils/adt/json.c

            else if (*s == '\\')  
            {  
                    /* OK, we have an escape character. */  
                    s++;  
                    len++;  
                    if (len >= lex->input_length)  
                    {  
                            lex->token_terminator = s;  
                            report_invalid_token(lex);  
                    }  
                    else if (*s == 'u')  
                    {  
                            int                     i;  
                            int                     ch = 0;

                            for (i = 1; i <= 4; i++)  
                            {  
                                    s++;  
                                    len++;  
                                    if (len >= lex->input_length)  
                                    {  
                                            lex->token_terminator = s;  
                                            report_invalid_token(lex);  
                                    }  
                                    else if (*s >= '0' && *s <= '9')  
                                            ch = (ch * 16) + (*s - '0');  
                                    else if (*s >= 'a' && *s <= 'f')  
                                            ch = (ch * 16) + (*s - 'a') + 10;  
                                    else if (*s >= 'A' && *s <= 'F')  
                                            ch = (ch * 16) + (*s - 'A') + 10;  
                                    else  
                                    {  
                                            lex->token_terminator = s + pg_mblen(s);  
                                            ereport(ERROR,  
                                                            (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),  
                                                             errmsg("invalid input syntax for type %s",  
                                                                            "json"),  
                                                             errdetail("\"\\u\" must be followed by four hexadecimal digits."),  
                                                             report_json_context(lex)));  
                                    }  
                            }

```

参考

json_lex_string@src/backend/utils/adt/json.c

《PostgreSQL 转义、UNICODE、与SQL注入》

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论