Easier way to serialize int value using JSONUtility ?
Hi, im looking for the easiest way to serialize a primitive value - in this case - a variable holding an int value.
According to JSON Serialization doc, the "easiest" way to do that is create a class/struct ... wait what? For simple int variable? So i tried this and it works:
using System;
[Serializable]
public struct SerializableInt
{
public int value;
}
and then elsewhere:
SerializableInt myInt = new SerializableInt();
myInt.value = 1;
Debug.Log (JsonUtility.ToJson(myInt)); // {"value":1}
but this looks as verbose as ever. Is there easier way to JSONize / StringIFY that?
Alternate approach not using JSON maybe?
My requirement is to pass string and parse it based on "dataType" something like:
switch (dataType) {
case "int": // parse as if string contained ints only - using SerializableInt here currently
case "float": // parse as if string contained float - SerializableFloat ???
case "Array": // parse as if string contained array - SerializableArray ???
case "Vector3": // fortunately we CAN parse the string using JSON directly.
etc
}
Answer by superpig · Jul 27, 2016 at 01:51 AM
JsonUtility doesn't support direct serialisation of primitives yet. I'm hoping to add it around the same time as direct serialisation of arrays.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075941im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Deserialize Json array with mixed types 0 Answers
How to work with Timestamp data in Unity 0 Answers
How to parse googlesheet json 0 Answers
JSON parse error, invalid value 1 Answer
Nested Dictionary to JSON by LitJson 0 Answers