Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object #22

Closed
Bluscream opened this issue May 16, 2020 · 8 comments

Comments

@Bluscream
Copy link

10:38:02.9510 | ERROR | Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object: PropertyName. Path ''.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
   at Newtonsoft.Json.Linq.JToken.ToObject[T]()
   at GModMountManager.Classes.Game..ctor(DirectoryInfo gameDir) in C:\Users\blusc\source\repos\GMod Mount Manager\Classes\GameInfo.cs:line 49
@shravan2x
Copy link
Owner

This appears to be an exception thrown from the Json.NET library. I suggest you try googling this instead, since it's not related to this library.

@Bluscream
Copy link
Author

Bluscream commented May 16, 2020

It is thrown right here
image

And the json that's printed by the log statement is

"GameInfo": {
  "game": "Black Mesa",
  "gamelogo": "1",
  "developer": "Crowbar Collective",
  "developer_url": "http://www.blackmesasource.com/",
  "type": "both",
  "SupportsDX8": "0",
  "SupportsXbox360": "1",
  "nomodels": "1",
  "nocrosshair": "1",
  "GameData": "bms.fgd",
  "InstancePath": "bms/mapsrc/",
  "FileSystem": {
    "SteamAppId": "362890",
    "ToolsAppId": "211",
    "SearchPaths": {
      "game+mod": "bms/bms_textures.vpk",
      "game": "|all_source_engine_paths|hl2/hl2_misc.vpk",
      "platform": "|all_source_engine_paths|platform/platform_misc.vpk",
      "game+mod+mod_write+default_write_path": "|gameinfo_path|.",
      "gamebin": "|gameinfo_path|bin",
      "game+game_write": "bms"
    }
  }
}

EDIT: I might be onto something here, if this isn't something that my logger changed, then it might be missing the initial "{" here? JSONlint reports the following:

Error: Parse error on line 1:
"GameInfo": {	"game": "Black 
----------^
Expecting 'EOF', '}', ',', ']', got ':'

I also switched from my own incomplete and short class to a generated one by quicktype.io
https://github.com/Bluscream/GMod-Mount-Manager/blob/master/Classes/GameInfo.cs

@Bluscream
Copy link
Author

Bluscream commented May 16, 2020

Also when googling this exception the results are fairly generic and the first result even just suggests not using a class which is entirely against why im using classes in the first place

Another answer suggests that it might be a bug in the parser library but i think it's something different there aswell

@Bluscream
Copy link
Author

Bluscream commented May 16, 2020

I also tried a bunch of other things, like using a real simplified version with just one property

    public class GameInfo
    {
        [Newtonsoft.Json.JsonProperty("game", NullValueHandling = NullValueHandling.Ignore)]
        public string Game { get; set; }
    }

Or wrapping it into another class because it might expect a parent?

    public class GameInfos
    {
        public GameInfo gameInfo { get; set; }
    }

this stuff slowly drives me insane, don't you have something like Gitter, Discord, Slack or Telegram? ^^

@Bluscream
Copy link
Author

Bluscream commented May 16, 2020

So what i gathered from the last 10 minutes looking stuff up and getting a light headache is that ".ToJson()" is just returning a jproperty which is supposedly just a part of the whole json which it would need to parse to an object?
Like what i get is

"GameInfo": { }

And what i would need is

{ "GameInfo": { } }

Is that correct? If yes, should i just slap { infront and append } ? Or is there a function to wrap a jprop into a jobj?

@shravan2x
Copy link
Owner

It seems you were trying to deserialize a JSON file with a property as its root instead of an object. The README for this library explains the difference root types in VDF are properties, as opposed to objects in traditional JSON..

I suggest you convert your JProperty into a JObject before serializing it.

@Bluscream
Copy link
Author

Bluscream commented May 16, 2020

root types in VDF are properties, as opposed to objects in traditional JSON.

i don't think most people know what you want to tell them when they see these buzzwords xD it requires quite a bit of behind the scenes knowledge of newton's library but yeah, i'm going to "{"+jproperty.ToString()+"}" before the json.ToObject<>()

@shravan2x
Copy link
Owner

You're right, the library assumes a certain familiarity with VDF and JSON formats. A cleaner approach might be new JObject(jproperty).

I'm closing this issue since you seem have found a solution. Feel free to reopen if you need more help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants