Groovy JsonBuilder将\ u0000附加到字符串

时间:2015-09-03 15:25:05

标签: json sockets groovy gpars jsonbuilder

我正在尝试为我正在制作的Unity3D游戏制作一个简单的UDP套接字服务器,而且我已经完成了它。我可以向它发送消息并阅读消息。但是当我试图将消息发送回客户端时(出于测试目的,目前),我得到一个BufferOverFlowException。

在发回数据之前,我正在使用groovy.json.JsonBuilder将其转换为json。数据结构非常简单:

[data:“Hello World”]

但无论出于何种原因,JsonBuilder都将其构建为

(Function instanceof Function)

\ u0000's持续一段时间。足够长,使我的1024字节长ByteBuffer溢出。

这是负责将数据发送回客户端的类:

{
    "data": "Hello World\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\..."
}

这是我得到的堆栈跟踪:

import groovy.json.JsonBuilder
import groovy.transform.CompileStatic
import groovyx.gpars.actor.DynamicDispatchActor

import java.nio.ByteBuffer
import java.nio.channels.DatagramChannel

@CompileStatic
class SenderActor extends DynamicDispatchActor{

    //takes message of type [data: Object, receiver: SocketAddress]
    void onMessage(Map message){

        println(message.data) //prints "Hello World"
        def json = new JsonBuilder([data: message.data]).toString()
        println("Sending: $json") //prints '{"data": "Hello World\u0000\u0000..."}'

        def channel = DatagramChannel.open()
        channel.connect(message.receiver as SocketAddress)

        def buffer = ByteBuffer.allocate(1024)
        buffer.clear()
        buffer.put(json.getBytes())
        buffer.flip()

        channel.send(buffer, message.receiver as SocketAddress)
    }
}

有问题的数据编码为UTF-8,如果有帮助的话。

这是负责将数据发送到服务器的客户端代码(用C#编写):

An exception occurred in the Actor thread Actor Thread 2
java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:189)
    at java.nio.ByteBuffer.put(ByteBuffer.java:859)
    at Croquet.Actors.SenderActor.onMessage(SenderActor.groovy:28)
    at Croquet.Actors.SenderActor$onMessage.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at Croquet.Actors.ProcessorActor$onMessage.call(Unknown Source)
    at groovyx.gpars.actor.impl.DDAClosure$_createDDAClosure_closure1.doCall(DDAClosure.groovy:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:292)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
    at groovy.lang.Closure.call(Closure.java:423)
    at groovy.lang.Closure.call(Closure.java:439)
    at groovyx.gpars.actor.AbstractLoopingActor.runEnhancedWithoutRepliesOnMessages(AbstractLoopingActor.java:293)
    at groovyx.gpars.actor.AbstractLoopingActor.access$400(AbstractLoopingActor.java:30)
    at groovyx.gpars.actor.AbstractLoopingActor$1.handleMessage(AbstractLoopingActor.java:93)
    at groovyx.gpars.util.AsyncMessagingCore.run(AsyncMessagingCore.java:132)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

我通过更改

解决了我的问题

def json = new JsonBuilder([data: message.data]).toString()

def json = new JsonBuilder([data: message.data]).toString().replace("\\u0000", "")