Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

JSON Reading and FixedString Question

Discussion in 'Project Tiny' started by collinmmckinney, Feb 1, 2021.

  1. collinmmckinney

    collinmmckinney

    Joined:
    Jun 25, 2018
    Posts:
    6
    Edit: This was my own error. I was accessing a field on the JSON object that didn't exist. The code below works fine!



    Hello!

    First off, thanks for all the great work on Project Tiny. The most recent UI update came at the perfect time for me.

    I am using the TinyJsonInterface to read from an external string source (it's a websocket but that shouldn't matter for the purpose of this question). I need to take a value from JSON and store it as data, ideally on an IComponentData, but when I try to assign it directly, I get an error about ToString not being implemented. I'm wondering: how can I take the FixedString4096 returned from the TinyJsonInterface's AsString() function and store it somewhere, such as on a data component, in order to access it later or use it to update UI, etc. Is that possible?

    To make this as clear as possible, I have:

    Code (CSharp):
    1. [GenerateAuthoringComponent]
    2. public struct ExampleComponent : IComponentData
    3. {
    4.     public FixedString4096 exampleString;
    5. }
    6.  
    7. ExampleComponent exampleComponent = GetSingleton<ExampleComponent>();
    8. exampleComponent.exampleString = exampleJsonInterface.Object["exampleField"].AsString();
    9. SetSingleton(clientInformation);
    For more context, the exact error is:
    "The exception message is: ToString is not implemented on System.Object for the Tiny profile. Please override ToString in the derived class if it is required."

    I understand that this might be me misunderstanding how FixedStrings work, so if someone could point me in the right direction that would be great.

    I need to hold onto a value that represents a client ID and a player name coming from a web server, so I really don't see any away around this issue if I can't hold onto the value. I would really appreciate any advice here.
     
    Last edited: Feb 1, 2021
  2. AbdulAlgharbi

    AbdulAlgharbi

    Unity Technologies

    Joined:
    Jul 27, 2018
    Posts:
    319
    Hi
    so try this
    Code (CSharp):
    1.        
    2.    
    3. [GenerateAuthoringComponent]
    4. public struct ExampleComponent : IComponentData
    5. {
    6.     public FixedString4096 exampleString;
    7. }
    8.  
    9. var exc = GetSingleton<ExampleComponent>();
    10. Debug.Log(exc.exampleString.ToString());
    11. var json = new TinyJsonInterface(@"{""key"": ""hi"", ""otherKey"": ""bye""}", Allocator.Temp);
    12. exc.exampleString = json.Object["otherKey"].AsString();
    13. json.Dispose();
    14. SetSingleton(exc);
    15.  
    check that TinyJsonInterface is getting the data correctly
    and make sure if you do debug.log you call exampleString.ToString()