2

Code in the main program

controller.modelToFile("passwords/test.json");
controller.deJSONizeModel("passwords/test.json");

Controller's modelToFile:

    public void modelToFile(String filename) {
        System.IO.File.WriteAllText(filename, JSONizeModel());
    }

Controller's JSONizeModel:

    public String JSONizeModel() {
        Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings();
        jss.Formatting = Newtonsoft.Json.Formatting.Indented;
        Newtonsoft.Json.Serialization.DefaultContractResolver dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
        jss.ContractResolver = dcr;
        return Newtonsoft.Json.JsonConvert.SerializeObject(model, jss);
    }

Controller's deJSonizeModel:

    public void deJSONizeModel(String json) {
        model = JsonConvert.DeserializeObject<Model>(json);
    }

Model:

class Model : IModel    {
    private IList<Password> passwords = new List<Password>();

<...>

File test.json:

{
  "passwords": [
    {
      "description": "example password 1",
      "password": "p1"
    },
    {
      "description": "example password 2",
      "password": "p2"
    },
    {
      "description": "This is the password",
      "password": "the password"
    }
  ]
}

Password class:

    public String description { get; set; }
    public String password { get; set; }

    public Password(String description, String password)  {
        this.description = description;
        this.password = password;
    }

Please help. The exception is Newtonsoft.Json.JSONReaderException.

Program log:

A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll
A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll
An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll
Additional information: Error parsing boolean value. Path '', line 0, position 0.
1
  • 1
    Could you add more information about the exception to the post? InnerException if any, stacktrace perhaps. Commented Jun 24, 2014 at 14:52

1 Answer 1

2

Solved it! The argument to deJSONizeModel is a FILE but JSON is required!

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it was your answer, but you posted it in the question section. I extracted it and posted it here on your behalf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.