Java对json格式的限制
我想通过 TCP 套接字传输一些数据库数据。数据被格式化为 JSON。
由于数据库大小可能会增长,恐怕 String 对象的最大大小不足以存储 JSON 格式的整个数据。
我在使用 DataOutput 函数 writeUTF()
传输数据时遇到了问题。
我应该怎么办?也许将数据库行转换为 CSV 并通过互联网逐行传输?或者我是否不需要担心字符串限制并通过获取字符串的字节、通过套接字传输它们并从目的地的字节重建字符串来解决 writeUTF() 问题?
I want to transfer some database data through a TCP socket. The data is formatted to JSON.
Since the database size might grow, I'm afraid that the String object maximum size will not be enough to store the entire data with JSON formatting.
I already had an problem transferring the data using the DataOutput function writeUTF()
.
What should I do? Maybe convert the database rows to CSV and transfer it through the Internet line by line? Or do I not need to worry about String limits and solve the writeUTF()
problem by getting the bytes of the String, transferring them through the socket and rebuilding the String from the bytes at the destination?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 字符串可能非常长 - 您不太可能遇到 String 类型本身的问题。如果您首先将字符串转换为二进制,然后使用 writeInt 写入字节数,然后写入字节本身,应该没问题。
writeUTF
的问题在于它使用writeShort
,因此它最多只能处理 64K 的数据。Java strings can be extremely long - you're unlikely to run into problems with the String type itself. If you convert the string to binary first, then use
writeInt
to write the number of bytes, then the bytes themselves, that should be fine. The problem withwriteUTF
is that it useswriteShort
, so it only handles up to 64K of data.