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 /
  • Help Room /
avatar image
0
Question by Olakehs · Jan 22, 2020 at 12:17 AM · serializationscriptableobjectjson

Serialization of ScriptableObject not hexadecimal (json)

Hi guys - still pretty new to Unity/programming in general

I can't seem to serialize my Scriptable Object using Json into a file that is hexadecimal. It shows up at something like in the attached image.

Here's my code for serialization using json:

         foreach (ScriptableObjectLevel so in SO)
         {
             string fileName = "/game_save/"+so.sceneName; 
             FileStream file = File.Create(Application.persistentDataPath + fileName);      
             string json = JsonUtility.ToJson(so);
             bf.Serialize(file,json);
             file.Close();
         }

Thanks in advance for any guidance

nonhex.png (8.6 kB)
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
1
Best Answer

Answer by Bunny83 · Jan 26, 2020 at 02:36 AM

I don't quite understand your question. JSON is just the javascript object notation which is a text based serialization format. It is just text. You seem to put that serialized text through the BinaryFormatter which makes not much sense since binary serialized text will still be text and is exactly what you see in your image. Hexadecimal is also just a text encoding for numbers. The JSON format does not support any kind of hexadecimal values.


If you wanted to ask how to store a ScriptableObject to a file at runtime so that a user can not directly read the content, that's something completely different. To make it harder to read your data you could of course convert your string into a byte array and convert the byte array to a hexadecimal string. This will exactly double the size of the resulting string. A usually better approach is to convert your byte array to base64 which doesn't have as much overhead. The data is still larger though by about 33%


Another way is to actually compress your data using any sort of compression algorithm. Of course most compression libraries are quite large. Alternatively you could use my Huffman encoder. I just added "EncodeFromUTF8" and "DecodeToUTF8" to directly convert a string to a huffman encoded byte stream. For short strings the result is usually a bit larger than the input string since the whole huffman tree is included in the byte stream. However unless you know how the data is actually encoded most people would have a hard time to decipher the data ^^. Note that this is not meant as any kind of security.


To sum up: We assume you have already serialized your ScriptableObject to json.

With my Huffman script you can use:

 byte[] data = B83.Compression.Huffman.EncodeFromUTF8(json);
 File.WriteAllBytes(Application.persistentDataPath + fileName, data);

to write the huffman encoded bitstream to the file.


To read the json back, use this:

 byte[] data = File.ReadAllBytes(Application.persistentDataPath + fileName);
 json = B83.Compression.Huffman.DecodeToUTF8(data);


If you want to just encode your json text as hexadecimal characters, you can use those two helpers from StackOverflow in a similar way

 // save
 string data = HexadecimalEncoding.ToHexString(json);
 File.WriteAllText(Application.persistentDataPath + fileName, data);

 // load
 string data = File.ReadAllText(Application.persistentDataPath + fileName);
 json = HexadecimalEncoding.FromHexString(data);


Likewise if you prefer base64 you can just use

 // save
 string data = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(json));
 File.WriteAllText(Application.persistentDataPath + fileName, data);
 
 // load
 string data = File.ReadAllText(Application.persistentDataPath + fileName);
 json = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(data));

Keep in mind that base64 encoding can easily be reverted, even with online tools like this one. The same is true for hexadecimal strings. As I said there is probably no converter for my custom huffman encoding, but if someone analysis your code he could figure it out.

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 Olakehs · Jan 09, 2021 at 11:14 PM 0
Share

Thanks for your comprehensive answer mate

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

204 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 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

Saving reference to a ScriptableObject with JsonUtility 1 Answer

jsonUtlility save/load ScriptableObjects 1 Answer

Nested dictionary serialization 0 Answers

Using ScriptableObjects for a Pokemon-like RPG 0 Answers

Question on SerializeField? 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