Search Unity

JsonUtility.ToJson return wrong content

Discussion in 'Scripting' started by lfwu, Mar 21, 2019.

  1. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    hey, when i use unity JsonUtility.ToJson to make my object to string,but on some mobile device, return wrong result like:
    {"hovered":[{"m_FileID":-696942,"m_PathID":0},{"m_FileID":-696926,"m_PathID":0},{"m_FileID":-696850,"m_PathID":0},{"m_FileID":-696818,"m_PathID":0},{"m_FileID":-696802,"m_PathID":0},{"m_FileID":236956,"m_PathID":0},{"m_FileID":236926,"m_PathID":0},{"m_FileID":236954,"m_PathID":0}]}

    my code is this:
    Code (CSharp):
    1. public class Pay
    2. {
    3.     [Serializable]
    4.     public class ActivityExtendData
    5.     {
    6.         public int activityID;
    7.         public int index;
    8.     }
    9.  
    10.     [Serializable]
    11.     public class ShopExtendData
    12.     {
    13.         public int shopID;
    14.     }
    15. }
    16.  
    17. // other place call the function
    18. Pay.ShopExtendData extData = new Pay.ShopExtendData();
    19. extData.shopID = 1002;
    20. string extStr = JsonUtility.ToJson(extData);
    the return string "extStr" most time return right json string, on some mobile device return wrong string:
    {"hovered":[{"m_FileID":-696942,"m_PathID":0},{"m_FileID":-696926,"m_PathID":0},{"m_FileID":-696850,"m_PathID":0},{"m_FileID":-696818,"m_PathID":0},{"m_FileID":-696802,"m_PathID":0},{"m_FileID":236956,"m_PathID":0},{"m_FileID":236926,"m_PathID":0},{"m_FileID":236954,"m_PathID":0}]}

    My unity version is:5.6.6f2, use il2cpp
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Can you indicate, which part of the string you think is wrong?
     
  3. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    the right format should be:"{"shopID":1002}"
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    There is nothing special about your ShopExtendData class.
    Meaning, if other classes are serialized, that you are probably overriding something somewhere.
    Or using different class as base.
    Too little show, to deduct anything.

    I don't see activityID in string either.
     
  5. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    I submit this problem the reason is the code is so simply, but just on some mobile device occurs this problem.
    the actvityID, i do not use "ActivityExtendData" this class, it will not contain "activityID" for sure.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    can you show us full extData class?
     
  7. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    the extData class is full, at this time, i find maybe the stack frame is wrong,look the full function i used:
    Code (CSharp):
    1.     private void OnItemClick(LoopBaseNode node, PointerEventData data)
    2.     {
    3.         RrechargeNode rnode = node.GetComponent<RrechargeNode>();
    4.  
    5.  
    6.         if (UserInfoManager.instance.GetCurrentTime() < rnode.curInfo.expireTime)
    7.         {
    8.             ScreenManager.instance.ShowTip(GetTextInfo(RechargelangEnum.rechargelang27));
    9.             return;
    10.         }
    11.         //... buy
    12.         m_curRechargeDataInfo = rnode.curInfo;
    13.         string productId = m_curRechargeDataInfo.data.rechargeId.ToString();
    14.         Pay.ShopExtendData extData = new Pay.ShopExtendData();
    15.         extData.shopID = m_curRechargeDataInfo.data.id;
    16.         string extStr = JsonUtility.ToJson(extData);
    17.         T7Game.UnitySDKInterface.U3dSdkInterface.GetPay().StartPay(productId, extStr);
    18.     }
    and i find the PonterEventData has member "hovered":
    Code (CSharp):
    1. namespace UnityEngine.EventSystems
    2. {
    3.     //
    4.     // Summary:
    5.     //     Event payload associated with pointer (mouse / touch) events.
    6.     public class PointerEventData : BaseEventData
    7.     {
    8.         //
    9.         // Summary:
    10.         //     List of objects in the hover stack.
    11.         public List<GameObject> hovered;
    12.      }
    13. }
    and the member "hovered" is a gameobject list
    thank you very much talk with me.@Antypodish
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    I can not see anything obvious here unfortunately, which could lead to an issue.

    Are you positive, you have only one OnItemClick method. What I am trying to confirm, if you maybe have other version of OnItemClick method, without shopID.

    I doubt this part of the string just disappear for no reason on some mobile devices.
    But if you can reproduce every time, then you could potentially fail file the bug. If that is indeed the case.

    Or maybe other thought, there was older file version, without that ID string, and can not be overwritten? Just thoughts.
     
    Last edited: Mar 21, 2019
  9. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    it always work correctly, but there are some device call this function will occur this problem, I know this because our server receive this wrong json string, so i just replace this statement "extStr = JsonUtility.ToJson(extData);" with string.format, our server never receive this wrong json. maybe the only problem is i used "nested class"? maybe just Unity guys can answer this question.

    you know we use lua can replace this function, that called hotfix, so i can not understand why this happened?
     
    Last edited: Mar 21, 2019
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    So best you can do now, if you got data, collect which devices are troubling and if you can replicate conditions..
     
  11. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    ok, thank you very much again, @Antypodish
     
  12. lfwu

    lfwu

    Joined:
    Jul 16, 2015
    Posts:
    7
    ok, I have find out that just my code bugs, I am sorry of wasting yours time!
     
    Antypodish likes this.