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
1
Question by malkere · Sep 09, 2017 at 06:36 AM · jsonnull

Why are my null JSON variables returning fully initialized?

I'm using a JsonUtility.ToJson and FromJson to save/load data. One part of the data for example is an InventorySlot class who has a public variable Item that by default is null. When creating a new character I specify their equipment as a list of 12 new InventorySlot() which means that there should be 12 null Item variables within the list.

However, when I FromJson the saved data back all of the Item variables within each InventorySlot are initialized as a new Item(), not as a null.

This breaks past all my null reference checks within the game.... I could rewrite the checks, not a HUGE deal, but is there an easy way to make Json save null as null? Some option I am missing?

Sample code: The InventorySlot class:

 [Serializable]
 public class InventorySlot {
 
     public int slotNumber = -1;
     public int itemAmount = 0;
     public Item item = null;
     public InventorySlotTypes slotType = InventorySlotTypes.Null;
 
 }

the InventorySlot being initialized during character creation (new save file creation):

         //initialize equipment
         for (int i = 0; i <= 18; i++) {
             this.equipmentSlots.Add(new InventorySlot());
             this.equipmentSlots[i].slotNumber = i;
             this.equipmentSlots[i].slotType = InventorySlotTypes.Equipment;
         }


There is no touching of the equipmentSlots after that, I then toJson, binaryformatter serialize to a file, deserialize on load, and fromJson back, and all of the item variables are initialized as new Item()..?

Comment
Add comment · Show 1
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 malkere · Sep 09, 2017 at 06:46 AM 0
Share

I found a forum post stating serialization will force it to be initialized... a problem I've run into clicking on monobehaviors with public variables will force initialization for the inspector... But I've been serializing/deserializing for months before I started using JSON and I never ran into a problem here?...

I think basic binaryformatting was not forcing initialization on class variables, where JSON requires it for its structuring...?

2 Replies

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

Answer by malkere · Sep 09, 2017 at 08:11 AM

It seems that JSON will force serialization on anything and everything it touches. Simply writing out binary data did not force serialization on null objects so I never ran into this before.

I've found that even calling ToJson with the equipment list being reference will force serialization on the entire list and all child variables even in runtime. Even copying the list without explicit recreation of any nested non-generics (ie: child lists) would serialize all said variables removing any and all null values in runtime (big probs if you are expecting things to be null).

Using a check on load to look for any Item with the default values and setting it to null before importing solved that. And making sure to use " new List(ListToSave) " on anything being pulled into the class ToJson will be called on (including all nested classes) prevented runtime serialization of anything I didn't want changed.

Comment
Add comment · 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
2

Answer by Bunny83 · Sep 09, 2017 at 08:14 AM

Unity's JsonUtility class behaves roughly the same as it's normal serialization system but will less features. So have a look at the script serialization documentation. Especially the section "When might the serializer behave unexpectedly?"

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 bottledgalaxy · Aug 06, 2020 at 09:32 PM 0
Share

Wow, just WOW! No support for null for custom classes That's $$anonymous$$d blowing...

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

71 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

Related Questions

About Json Datapath. 1 Answer

LitJson null bug 0 Answers

Recieving Null when I should not be with JSONParse.js 0 Answers

how can I check if an object is null? 4 Answers

what is null 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