Fabric.js反序列化

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31519989/article/details/99562954

Fabric.js反序列化

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Fabric.js反序列化</title>
</head>
<style>
    html, body {
        margin:0;
        padding:0;
    }
    .container {
        height:100%;
        width:100%;
        border: 1px solid green;
        position:fixed;
    }
</style>
<body>
<canvas class="container" id="canvas"></canvas>

<script src="lib/fabric.js"></script>
<script>
    var canvas = new fabric.Canvas('canvas');
    var json = {"version":"3.3.0","objects":[{"type":"rect","version":"3.3.0","originX":"left","originY":"top","left":0,"top":0,"width":100,"height":100,"fill":"red","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeDashOffset":0,"strokeLineJoin":"miter","strokeMiterLimit":4,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","paintFirst":"fill","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"rx":0,"ry":0}]};
    canvas.clear();
    canvas.loadFromJSON(json, canvas.renderAll.bind(canvas));
</script>
</body>
</html>

在这里插入图片描述

展开阅读全文

序列化报错

02-04

public byte[] SerializeBinary(object obj)rn rn IFormatter formatter = new BinaryFormatter();rn MemoryStream stream = new MemoryStream();rn formatter.Serialize(stream, obj);rn byte[] result = stream.ToArray();rn stream.Close();rn return result;rn rnrn public object DeSerializeBinary(MemoryStream stream)rn rn IFormatter formatter = new BinaryFormatter();rn object obj = formatter.Deserialize(stream);rn stream.Close();rn return obj;rn rnrn发送如下:rn this.sockUDP1.Send(this.ServerIP, this.ServerPort, msga.SerializeBinary(msg));rnrn接收如下:rnrn System.IO.MemoryStream MS = new System.IO.MemoryStream(Data);rn ClassSerializers msga = new ClassSerializers();rn ClassMsg ma = msga.DeSerializeBinary(MS) as ClassMsg;rnrnrn报错如下:rnrn未处理 System.Reflection.TargetInvocationExceptionrn Message="调用的目标发生了异常。"rn Source="mscorlib"rn StackTrace:rn 在 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)rn 在 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)rn 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)rn 在 System.Delegate.DynamicInvokeImpl(Object[] args)rn 在 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)rn 在 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)rn 在 System.Threading.ExecutionContext.runTryCode(Object userData)rn 在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)rn 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)rn 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)rn 在 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)rn 在 System.Windows.Forms.Control.InvokeMarshaledCallbacks()rn 在 System.Windows.Forms.Control.WndProc(Message& m)rn 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)rn 在 System.Windows.Forms.ContainerControl.WndProc(Message& m)rn 在 System.Windows.Forms.Form.WndProc(Message& m)rn 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)rn 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)rn 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)rn 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)rn 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)rn 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)rn 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)rn 在 System.Windows.Forms.Application.Run(Form mainForm)rn 在 klm8_ser.Formmain.Main() 位置 E:\klm8\klm8qq\klm8_ser\klm8_ser\Formmain.cs:行号 20rn 在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)rn 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)rn 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()rn 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)rn 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)rn 在 System.Threading.ThreadHelper.ThreadStart()rn 论坛

Gson序列化

05-29

在使用google的Gson类解析json格式的字符串为java bean的时候遇到了一个问题,The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@1dd21dd2 failed to deserialize json object,given the type java.util.List。java bean的数据格式是这样的:public class Tradern private String adjustFee;rn private Long alipayId;rn private List orders;rnrn public List getOrders()rn rn return orders;rn rn public void setOrders(List orders)rn rn this.orders = orders;rn rnrn public String getAdjustFee()rn rn return adjustFee;rn rn public void setAdjustFee(String adjustFee)rn rn this.adjustFee = adjustFee;rn rn public Long getAlipayId()rn rn return alipayId;rn rn public void setAlipayId(Long alipayId)rn rn this.alipayId = alipayId;rn rnrnpublic class Orderrn private String adjustFee;rn private String buyerNick;rn public String getAdjustFee()rn rn return adjustFee;rn rn public void setAdjustFee(String adjustFee)rn rn this.adjustFee = adjustFee;rn rn public String getBuyerNick()rn rn return buyerNick;rn rn public void setBuyerNick(String buyerNick)rn rn this.buyerNick = buyerNick;rn rnrn其中Trade类中有个List,里面存放的就是Order,只是没有使用泛型,java bean中的属性没有全部给出,只是跟异常相关的属性(Order),然后调用Gson的方法去将json字符串转化为Trade对象,Trade trade = gson.fromJson(json, Trade.class);然后就抛出了上面提到的异常。也就是这种问题,一个java bean中有Collection类型的属性,怎么利用Gson实现反序列化?求大神指导! 论坛

没有更多推荐了,返回首页