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 RealMTG · Jun 11, 2016 at 04:00 PM · serializationjson

Stop specific fields from being serialized by JSON utility

Hi

I am saving some info about objects in a .json file and that object has some "editor only" fields to only set where the object should send some messages but these fields are also being serialized and added to the .json file. It isn't a "problem", really, but the fields are taking up some unnecessary space. I tried adding the "[System.NonSerializable]" attribute to the fields but that just disables them from being viewed at all and caused problems with the custom editor script I am using for this.

So any help with this would be greatly appreciated!

(I'm also not showing any code because I don't see any point in showing the code I have. It is basically a class with a few variables and then JsonUtility.ToJson())

Comment
Add comment · Show 2
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 edwon · Jan 06, 2017 at 11:16 PM 0
Share

I have the same question

avatar image Arcana96 · Jan 07, 2017 at 05:45 AM 0
Share

I don't know if this would work but .NET json serializer had an attribute [JsonIgnore] that you put above the field you want to exclude.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by $$anonymous$$ · Aug 13, 2017 at 10:35 AM

use the [System.NonSerialized] attribute.

[System.NonSerialized] public GameObject myGameObject;

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
1

Answer by edwon · Jan 07, 2017 at 10:25 AM

Yes I found the [JsonIgnore] attribute before, but it doesn't appeart to exist in Unity. Perhaps it's because Unity has it's own Json Utility that seems to be different than the standard .NET

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
1

Answer by Bunny83 · Jan 07, 2017 at 11:07 AM

Unity's JsonUtility does work exactly the same as the usual serialization inside the editor. So by default it will only serialize public variables. With the SerializeField attribute you can make it serialize private variables and with NonSerialized you can prevent a variable from being serialized.

However if you want a certain variable be serialized in the editor but not by the JsonUtiltiy, that's not possible as they work exactly the same. The JsonUtility is ment to provide a serialization method at runtime. If you just want to serialize a subset of a class, you would have to create a seperate data class with the fields you want to serialize. There's no other way. The only alternative would be to manually parse the created JSON text and remove the unwanted information from there.

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

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

50 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

Related Questions

How do I go about deserializing a json array? 3 Answers

What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers

UnityEngine.Object field pointing to Project file gets serialized by JSON as instance ID. After deserialization field points to different Object. Why? 1 Answer

JsonUtility and Arrays [Error - "JSON must represent an object type"] 2 Answers

Anyone successful using native JSON libs with AS3? 0 Answers


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