Skip to content

NotYours180/msgpack-unity3d

 
 

Repository files navigation

Introduction

This package provides API for data serialization/deserialization into MessagePack and JSON formats.

Supported Platforms:

  • PC/Mac
  • iOS
  • Android
  • WebGL

API

  • Json
    • Serialize
    • SerializeToString
    • Deserialize
  • MsgPack
    • Serialize
    • Deserialize

##Installation Nuget

PM> Install-Package GameDevWare.Serialization 

Unity3D

Json + MessagePack Serializer

Example

Serialize object into Stream using MessagePack serializer:

var outputStream = new MemoryStream();
MsgPack.Serialize(new { field1 = 1, field2 = 2 }, outputStream);

Deserialize object from Stream using MessagePack serializer:

Stream inputStream;
MsgPack.Deserialize(typeof(MyObject), inputStream); -> instance of MyObject
// or
MsgPack.Deserialize<MyObject>(inputStream); -> instance of MyObject

Mapping Types

MessagePack/Json serializer is guided by Data Contract rules. Its behaviour can be changed with DataContract, DataMember, IgnoreDataMember attributes.

Attributes can be from System.Runtime.Serialization.dll or your attributes with same names.

Only public fields/properties can be serialized.

Supported Types

  • Primitives: Boolean, Byte, Double, Int16, Int32, Int64, SBytes, Single, UInt16, UInt32, UInt64, String
  • Standard Types: Decimal, DateTimeOffset, DateTime, TimeSpan, Guid, Uri, Version, DictionaryEntry
  • Unity3D Types: Bounds, Vector, Matrix4x4, Quaternion, Rect, Color ...
  • Binary: Stream, byte[]
  • Lists: Array, ArrayList, List, HashSet and any other IEnumerable types with Add method.
  • Maps: Hashtable, Dictionary<K,V>, and other IDictionary types
  • Nullable types
  • Enumeration
  • Objects

Custom Type Serializers

To implement a custom TypeSerializer you need to inherit it from TypeSerializer and override Deserialize and Serialize methods.

public sealed class GuidSerializer : TypeSerializer
{
	public override Type SerializedType { get { return typeof(Guid); } }

	public override object Deserialize(IJsonReader reader)
	{
		var guidStr = reader.ReadString(advance: false); // advance mean 'call reader.NextToken' after ReadString
		var value = new Guid(guidStr);
		return value;
	}

	public override void Serialize(IJsonWriter writer, object valueObj)
	{
		var value = (Guid)valueObj; // you never get null here
		var guidStr = value.ToString();
		writer.Write(guidStr);
	}
}

Then you need to register your class in Json.DefaultSerializers collection or mark it with TypeSerializerAttribute.

Extra Type Information

There is additional type information with each serialized object. It increases size of the serialized data. If you do not want to store object's type information, specify SuppressTypeInformation when calling Serialize method.

MsgPack.Serialize(value, stream, SerializationOptions.SuppressTypeInformation);

If you want to ignore type information when deserializing an object, specify SuppressTypeInformation when calling Deserialize method.

MsgPack.Deserialize(typeof(MyObject), stream, SerializationOptions.SuppressTypeInformation);

Contacts

Please send any questions at support@gamedevware.com

License

If you embed this package, you MUST provide a link and warning about embedded package in the description of your package.

Asset Store Terms of Service and EULA

About

MessagePack serializer for Unity3D

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.9%
  • Batchfile 0.1%