使用存储为字符串的二进制数据序列化对象

时间:2015-08-09 13:51:44

标签: c# json.net

我正在使用NamedPipeStream,客户端和服务器,我正在从客户端向服务器发送数据,数据是包含二进制数据的序列化对象。

当服务器端收到数据时,我尝试反序列化收到的数据: TransmitDataCommand dataR = JsonConvert.DeserializeObject(e.Message));

我尝试将数据保存在base64字符串而不是byte []中,但仍无法正常工作!!

但我得到一个例外:

  

“未终止的字符串。预期的分隔符:”。路径'数据',第1行,   位置1024。“

为什么呢?如何解决这个问题:(?

从客户发送: 写(lTransmitDataCommand.Message())

管道读取数据: OnReceivedMessage(this,new ReceivedMessageEventArgs(Encoding.ASCII.GetString(message)));

并尝试:JsonConvert.DeserializeObject(e.Message)); //这个异常发生了!

对象:

[Serializable]
public abstract class AgentConnectorAbstractCommand
{
    public virtual eAGENTCONNECTOR_CommandOpcode Opcode { get; set; }
    public virtual void Dispose()
    {
        return;
    }
    public abstract string Message();
}

public enum eComandType{
    /// <summary>
    /// This command is from client to Agent
    /// </summary>
    eCLIENT2AGENT_CMD=0,
    /// <summary>
    /// This command is from agent to client
    /// </summary>
    eAGENT2CLIENT_CMD=1,
    /// <summary>
    /// This command is from client to Agent
    /// </summary>
    eRESPONSE_CMD=2,
}
[Serializable]
public class TransmitDataCommand : AgentConnectorAbstractCommand
{
    public TransmitDataCommand()
    {
        IsBinary = false;
    }
    private eAGENTCONNECTOR_CommandOpcode _Opcode = eAGENTCONNECTOR_CommandOpcode.eTRANSMITDATA;
    public override eAGENTCONNECTOR_CommandOpcode Opcode { get { return _Opcode; } }

    /// <summary>
    /// Define what is the type of the data (is it command or response)
    /// </summary>
    public eComandType ComandType { get; set; }

    /// <summary>
    /// indicate if the data is byte array or text
    /// </summary>
    public bool IsBinary { get; set; }

    /// <summary>
    /// Hold the byte data
    /// </summary>
    public byte[] Data
    {
        get
        {
            if ((DataTxT == null)||(IsBinary==false))
            {
                return null;
            }
            return Convert.FromBase64String(DataTxT);
        }
        set
        {
            if (value == null)
            {
                IsBinary = false;
                this.DataTxT = null;
                return;
            }
            try
            {
                IsBinary = true;
                //keep a clone of the image to protect from any changes outside!!!
                this.DataTxT = Convert.ToBase64String(value);
            }
            catch { }
        }
    }

    /// <summary>
    /// Hold the string data
    /// </summary>
    private string lTextData = null;
    public string DataTxT
    {
        get
        {
            return lTextData;
        }
        set
        {
            this.lTextData = value;
        }
    }

    public override void Dispose()
    {
        lTextData= null;
        GC.Collect();
    }
    /// <summary>
    /// Return serialized object
    /// </summary>
    public override string Message()
    {
        TransmitDataCommand l = new TransmitDataCommand
        {
            Opcode = this._Opcode,
            ComandType = this.ComandType,
            DataTxT = this.DataTxT,
            IsBinary = this.IsBinary
        };
        return JsonConvert.SerializeObject(l);
    }
    }

2 个答案:

答案 0 :(得分:0)

您的课程中存在很多设计问题。为什么不序列化当前实例而不是创建新实例? 无论如何,将JsonIgnoreAttribute添加到Data属性。

答案 1 :(得分:0)

问题在于我发送数据的方式,我使用StreamWriter - 当发送足够长的消息时,结果将是管道流上的多个Write调用=​​&gt;多个单独的数据消息。

因此,要将整个消息作为单个消息发送,我们必须使用Pipe.write本身使用单个Write调用。例如:

var data = Encoding.ASCII.GetBytes(JsonString);
Pipe.Write(data, 0, data.Length);

而不是StreamWriter.Writelen(JsonString)

了解更多信息,感谢Luaan,请参阅:NamedPipeServerStream receive MAX=1024 bytes, why?