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 /
This question was closed Aug 11, 2019 at 05:39 PM by Sazails for the following reason:

Other

avatar image
0
Question by Sazails · Aug 11, 2019 at 04:57 PM · saveloadclassjsondata

How to save and load any data type?

Hello, I have seen this somewhere in games where you can save and load any type of data instead of creating separate ones for each class. For example I have a Save() function:

 public static void Save(someclass)
 {
     I want to get the "someclass" data and save it to a json file.
 }
 
 public static (someclass return) Load(file)
 {
     Load the "file" then read it and return the "someclass".
 }

So how do I do something like this?

Thank you.

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

  • Sort: 
avatar image
0

Answer by sacredgeometry · Aug 11, 2019 at 05:32 PM

The industry standard JSON serialiser for c# is Newtonsoft.JSON, the frameworks file writing classes are in the IO namespace.

Example

     public T ReadFileAs<T>(string path) {
        var json = File.ReadAllText(path)
        return  JsonConvert.DeserializeObject<T>(json);
     }
     
     public void SaveFile(string path, Object obj) {
         var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
         File.WriteAllText(path, json);
     }
 

So the steps are:

For saving

  1. Serialise Object to json string

  2. Persist String to txt file

For reading saved file:

  1. Load File and get string from it

  2. Deserialise string to object

There isnt really much to it.

https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

and

https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files

or

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-from-a-text-file

Comment
Add comment · Show 4 · 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 Sazails · Aug 11, 2019 at 05:35 PM 0
Share

The question is how to save and load any type of data which I could of provided more information on what I really want.

Anyways I have found it to be something like this:

     public static void Save<T>(string fileName, object obj)
     {
 
     }
 
     public static T Load<T>(string fileName)
     {
 
     }

Hopefully this is the right methods however you might know if this is the way of doing this?

avatar image sacredgeometry Sazails · Aug 11, 2019 at 05:37 PM 0
Share

I thought you wanted to know how to actually do it not how to format generic methods.

Either way. thats not entirely necessary for the method I explained.

avatar image Sazails sacredgeometry · Aug 11, 2019 at 05:38 PM 0
Share

Question was kinda misleading as I already know and have used JSON few times. Thanks anyways for your time.

Show more comments

Follow this Question

Answers Answers and Comments

116 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

Related Questions

Loading a list into unity 0 Answers

Saving GameObject to file 2 Answers

Saving/Loading data to Windows Phone 8 3 Answers

How can I save and load a player's position? 5 Answers

SimpleJSON array problem 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