Announcement

Collapse
No announcement yet.

System.InvalidOperationException when serializing using JSON.NET

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    System.InvalidOperationException when serializing using JSON.NET

    I'm developing an app that will run on a Android mobile using Cordova.

    While testing it (still in a browser) and serializing an array of objects using JSON.NET an exception is raised:
    uncaught exception: System.InvalidOperationException: Operation is not valid due to the current state of the object.
    ctor@http://localhost:8000/js/bridge/bridge.js:5436:31
    $ctor1@http://localhost:8000/js/bridge/bridge.js:5507:17
    ctor@http://localhost:8000/js/bridge/bridge.js:33172:17
    validateReflectable@http://localhost:8000/js/bridge/newtonsoft.json.js:335:35
    SerializeObject@http://localhost:8000/js/bridge/newtonsoft.json.js:791:29
    SerializeObject@http://localhost:8000/js/bridge/newtonsoft.json.js:710:42
    Save@http://localhost:8000/js/bridge/SeV.PTM.Client.MobileBridge.js:700:38
    Save@http://localhost:8000/js/bridge/SeV.PTM.Client.MobileBridge.js:775:17
    SaveButtonClick@http://localhost:8000/js/bridge/SeV.PTM.Client.MobileBridge.js:1064:17
    bind/fn<@http://localhost:8000/js/bridge/bridge.js:1798:38
    makeFn/<@http://localhost:8000/js/bridge/bridge.js:1656:36
    dispatch@http://localhost:8000/libs/jquery-3.3.1.min.js:2:41720
    add/y.handle@http://localhost:8000/libs/jquery-3.3.1.min.js:2:39774
    The code the raises the exception in the call to SerializeObject.
        private static void Save(DevicePhotoGroup[] values)
        {
          if (values == null)
            values = new DevicePhotoGroup[0];
    
          var jsonValues = JsonConvert.SerializeObject(values);
      /*... omitted */
    }
    If it may help, trying to pinpoint the source of the exception, I see that the method
    SerializeObject: function (obj, formatting, settings, returnRaw, possibleType, dictKey)
    gets called a first time with obj = values and a second time with obj = values[0]
    It then somhow check obj and its type, until it call
    Newtonsoft.Json.JsonConvert.validateReflectable(ty pe);
    It detect that
    ignoreMetaData = false
    nometa = true
    then raises the exception...

    The same code worked a few days back and I can't find what's causing this...
    I've tried to reproduce the error in deck.net, copy and pasting code from my project... to no avail :-(

    Does any of you have a suggestion? A hint? I'm quite lost... thank you!
    Lorenzo

    #2
    Hello @LorenzoVegetti!

    Can you try to reproduce this issue in a Deck so we could take a closer look? I don't have much clue with the information you provided.

    Comment


      #3
      Are you sure that metadata is generated for your types which you try to serialize?

      Comment


        #4
        I ran into the exact same issue, with:

        ignoreMetaData = false
        nometa = true

        triggering an exception. Vladimir's comment helped me figure out the root cause -- my html file was missing the ".meta.js" files. Adding:

        <script src="MyApp.meta.js"></script>

        fixed the issue for me.

        Comment

        Working...
        X