Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NonSerialized data fields are set to null. #67

Open
yjchun opened this issue Oct 9, 2024 · 0 comments
Open

NonSerialized data fields are set to null. #67

yjchun opened this issue Oct 9, 2024 · 0 comments

Comments

@yjchun
Copy link

yjchun commented Oct 9, 2024

When deserializing a class, non serialized member fields are set to null, even when it is initialized in default constructor or member initializer. Member fields such as NonSerialized decorator or private members are all set to null.

Is this intended behaviour?
Example code I used is attached.
Thank you.

using System;
using System.Collections.Generic;
using UnityEngine;
using TriInspector;
using OdinSerializer;

[Serializable]
class SerializerTestClass
{
	public string message = "Message initialized in Class";
	[NonSerialized]
	public string message2 = "NonSerialized message initialized in Class";
	internal string message3 = "Private message initialized in Class";

	public SerializerTestClass()
	{
		methodAction = testMethod;
	}

	public Action action = () => {
		Debug.Log("Action initialized in Class");
	};
	public Action methodAction = staticTestMethod;

	public void PrintTest()
	{
		Debug.Log(message);
		Debug.Log(message2);
		Debug.Log(message3);
		action();
		methodAction();
	}

	void testMethod()
	{
		Debug.Log("MethodAction initialized in Class");
	}
	static void staticTestMethod()
	{
		Debug.Log("staticTestMethod initialized in Class");
	}
}

public class SerializerTest : MonoBehaviour
{
	SerializerTestClass testClass = new SerializerTestClass();
	void Awake()
	{
	}

	void MethodAction()
	{
		Debug.Log("MethodAction called");
	}

	[Button]
	void Serialize()
	{
		Debug.Log("=============== Serialize Test =================");
		Init();
		var data = Serialize< SerializerTestClass>(testClass, false);
		Debug.Log("Serialized Data: " + System.Text.Encoding.UTF8.GetString(data));
		testClass = Deserialize<SerializerTestClass>(data, false);

		testClass.PrintTest();
	}

	[Button]
	void Init()
	{
		testClass = new();

		testClass.message = "Message initialized in Constructor";
		testClass.message2 = "NonSerialized message initialized in Constructor";
		testClass.message3 = "Private message initialized in Constructor";
		testClass.action = () =>
		{
			Debug.Log("Action called");
		};
		testClass.methodAction = MethodAction;

	}

	public static byte[] Serialize<T>(in T value, bool binary = true)
	{
		return SerializationUtility.SerializeValue(value, binary ? DataFormat.Binary : DataFormat.JSON);
	}
	public static T Deserialize<T>(byte[] bytes, bool binary = true)
	{
		return SerializationUtility.DeserializeValue<T>(bytes, binary ? DataFormat.Binary : DataFormat.JSON);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant