将bytea插入到PostgreSQL json类型中 [英] Insert bytea into postgresql json type

查看:345
本文介绍了将bytea插入到PostgreSQL json类型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以postgresql json类型作为json列之一插入字节数组,但没有成功.是否有可能?有人有例子吗?

I am trying with no success to insert a byte array in a postgresql json type as one of the json columns. is it possible? does anyone have an example?

推荐答案

您至少不能做到这一点. JSON仅允许3种基本基本数据类型:数字,字符串和布尔值.其他所有内容都必须序列化为字符串或数字.

You can't, at least natively. JSON only allows 3 basic primative data types: number, string, and boolean. Everything else must be serialized to a string or number.

这意味着您有三个基本选项:

This means that you have three basic options:

  1. 序列化为十六进制.优点是,如果需要,可以很容易地在PostgreSQL中将其转换为Bytea.

  1. Serialize to hexadecimal. The advantage is that it becomes easy to turn into a bytea in PostgreSQL if you need it.

序列化为base64.这样做的好处是占用更少的空间.

Serialize to base64. The advantage here is that it uses up less space.

序列化为数字数组.我认为这不是首选,因为很难将每个数字限制在0到255之间.

Serialize to a number array. This is not preferred in my view since it is hard to constrain each number to between 0 and 255.

这篇关于将bytea插入到PostgreSQL json类型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆