Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is System.Serializable used here?

Discussion in 'Scripting' started by joe7987, Sep 18, 2020.

  1. joe7987

    joe7987

    Joined:
    Sep 12, 2013
    Posts:
    49
    Why is System.Serializable used in this code? I've been tearing this down, line-by-line, to try to understand every piece of it, and this is the last thing I don't understand.

    I do understand that system.serializable is used to convert things to bytes. I do know that some people use it so that they can modify things in the inspector. This particular piece of code has nothing to do with the inspector. I'm not sure why these classes need to be converted into bytes in order to work. Does it have something to do with JSON? The HTTP Request?

    It's used in two places. In both places, I've used ***************************************** so you can more easily find it.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. using System;
    6. using System.IO;
    7. using UnityEngine.Networking;
    8. using System.Net;
    9.  
    10. public class WeatherScript : MonoBehaviour
    11. {
    12.  
    13.     private const string API_KEY = "<keygoeshere>";
    14.     public string zip = "<zipgoeshere>";    
    15.  
    16.     public GameObject weatherParticles;
    17.    
    18.  
    19.     void Start()
    20.     {
    21.            
    22.         string weather = GetWeather().weather[0].main;
    23.  
    24.         Debug.Log(weather);
    25.     }
    26.  
    27.  
    28.  
    29. [B]//*******************************************************************************************
    30.     [System.Serializable]   [/B]
    31.     public class Weather    
    32.     {
    33.         public int id;
    34.         public string main;
    35.         public string description;
    36.         public string icon;
    37.     }
    38.  
    39. [B]//*******************************************************************************************
    40.     //[System.Serializable][/B]
    41.     public class WeatherInfo    
    42.     {
    43.         public int id;
    44.         public string name;            
    45.         public List<Weather> weather;  
    46.     }
    47.  
    48.  
    49.     private WeatherInfo GetWeather()  
    50.     {
    51.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.openweathermap.org/data/2.5/weather?zip=" + zip + "&APPID=" + API_KEY));
    52.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    53.         StreamReader reader = new StreamReader(response.GetResponseStream());
    54.         string unformattedData = reader.ReadToEnd();                      
    55.         WeatherInfo jsonInfo = JsonUtility.FromJson<WeatherInfo>(unformattedData);
    56.         return jsonInfo;  
    57.     }
    58.  
    59.    
    60. }
    61.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    To Save Data is need to be Serializable,
    if it has anything todo with the HTTP Request i am not sure :eek:
     
  3. jasrei

    jasrei

    Joined:
    Apr 19, 2018
    Posts:
    15
    Without testing I don't know for sure, but most serialization libraries do not require the Serializable attribute. If this is literally the only code then it may not be necessary.

     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Unity's JSON package is essentially "JSON super lite" because it doesn't support fundamental structures such as Dictionaries. AFAIK it also requires you to flag stuff System.Serializable, otherwise it won't consider it.

    Generally I do not recommend using the in-built JSON from Unity; it's just too limiting. It's focused on performance and simplicity rather than completeness of implementation.

    Instead, I recommend JSON .NET, available free in a .unitypackage form from the Unity asset store.
     
  5. joe7987

    joe7987

    Joined:
    Sep 12, 2013
    Posts:
    49
    It does not seem to work if I comment out the line that is currently uncommented.