Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Igor_Vasiak · Feb 16, 2018 at 08:26 PM · scripting problemsavejsonint

How to store a nullable int into a .json file?

I'm trying to store an 'int?' - a.k.a. Nullable - into a .json file, but until now I haven't found any light on this. Could someone enlighten me, please?

I use the good 'n old System.IO to save to .json file format.

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ElijahShadbolt · Feb 17, 2018 at 05:13 AM

This script works by representing a nullable integer as a JSON string value instead of the normal JSON integer or null values. To do this without access to the internal workings of the parser (e.g. UnityEngine.JSONUtility), I made an intermediary class which contained string fields instead of nullable fields, and then copied the values across to a class with nullable fields.

ExampleScript.cs

 using UnityEngine;
 
 public class ExampleScript : MonoBehaviour
 {
     public class MyClassIntermediate
     {
         public int normalInt;
         public string nullableInt1;
         public string nullableInt2;
 
         // empty constructor
         public MyClassIntermediate() { }
 
         // from normal class
         public MyClassIntermediate(MyClass other)
         {
             this.normalInt = other.normalInt;
             this.nullableInt1 = JsonUtilityExtensions.NullableIntToJson(other.nullableInt1);
             this.nullableInt2 = JsonUtilityExtensions.NullableIntToJson(other.nullableInt2);
         }
     }
 
     public class MyClass
     {
         public int normalInt;
         public int? nullableInt1;
         public int? nullableInt2;
 
         // empty constructor
         public MyClass() { }
 
         // from intermediate class
         public MyClass(MyClassIntermediate other)
         {
             this.normalInt = other.normalInt;
             this.nullableInt1 = JsonUtilityExtensions.JsonToNullableInt(other.nullableInt1);
             this.nullableInt2 = JsonUtilityExtensions.JsonToNullableInt(other.nullableInt2);
         }
     }
 
     private void Awake()
     {
         string json;
         MyClassIntermediate intermediate;
         MyClass myClass;
 
 
         // SERIALIZATION
 
         myClass = new MyClass();
         myClass.normalInt = 1;
         myClass.nullableInt1 = -2;
         myClass.nullableInt2 = null;
 
         // do intermediary stuff to represent nullable types as strings.
         intermediate = new MyClassIntermediate(myClass);
 
         // serialize normally.
         json = JsonUtility.ToJson(intermediate);
 
         Debug.Log("Serialized json: " + json);
 
 
         // DESERIALIZATION
 
         // parse normally.
         intermediate = JsonUtility.FromJson<MyClassIntermediate>(json);
 
         // do intermediary stuff to get nullable types from strings.
         myClass = new MyClass(intermediate);
 
         Debug.Log("Deserialized Data:");
         // use the deserialized data.
         Debug.Log("normalInt: " + myClass.normalInt);
         Debug.Log("nullableInt1: " + (myClass.nullableInt1 == null ? "null" : myClass.nullableInt1.ToString()));
         Debug.Log("nullableInt2: " + (myClass.nullableInt2 == null ? "null" : myClass.nullableInt2.ToString()));
     }
 }

 public static class JsonUtilityExtensions
 {
     // The json string to represent null values.
     public const string JsonNull = "null";
 
     // Serializes a nullable integer to a json string.
     public static string NullableIntToJson(int? nullable)
     {
         if (nullable.HasValue)
             return nullable.Value.ToString();
         else
             return JsonNull;
     }
 
     // Parses a json string containing an integer or null.
     public static int? JsonToNullableInt(string json)
     {
         if (json == JsonNull)
             return null;
         else
         {
             try
             {
                 return int.Parse(json);
             }
             catch // error, was not an integer or null!
             {
                 Debug.LogError("JSON parsing error: Expected nullable integer.");
                 return null;
             }
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Igor_Vasiak · Feb 17, 2018 at 03:32 PM 1
Share

That solved it, thanks! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

LitJson load float 2 Answers

Odd change in behaviour after I rearranged project files 0 Answers

Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer

How can I save data to a text/json file and read it back and then assign the values back to the variables ? 2 Answers

Position Translation problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges