0

Even using an example from the Documentation, I still can't find a way to successfully serialize to a file.

Code:

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Threading;

namespace TestProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Item i = new Item
            {
                Username = "user",
                Email = "[email protected]",
                Password = "password"
            };


            File.WriteAllText(@"C:\users\user1.json", JsonConvert.SerializeObject(i));

            using (StreamWriter file = File.CreateText(@"C:\users\user1.json"))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(file, i);
            }

        }
    }
}

Item.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject
{
    public class Item
    {
        public string Email { get; set; }
        public string Password { get; set; }
        public string Username { get; set; }
    }
}

When I run Program.cs, it shows no errors, but the JSON does not show in the file.

4
  • You're writing to the same file twice, which makes it hard to tell which part is failing; have you tried assigning JsonConvert.SerializeObject(i) to a variable and checking it during debugging? Commented Nov 25, 2015 at 3:18
  • Your code is working good at my computer. Commented Nov 25, 2015 at 3:19
  • Yeah users is a protected folder... And it's not going to always be c:\users for everyone.. E.g. my users folder is on p:\os\users and my temp folder is p:\temp.. and my OS is on c:\ e.g. c:\windows. Long story short you should use environment variables, e.g. @"%homepath%\user.json" which would put it in c:\users\user1\user.json in your example "the current logged in user's user folder" Commented Nov 25, 2015 at 3:31
  • Sorry, the "users" folder was supposed to be for the database, not the system's users. I forgot that Windows contains a Users folder at the root of the C: drive. Sorry for the confusion. Commented Nov 25, 2015 at 20:43

2 Answers 2

1

When I run your code I get a an exception writing to the users folder. Changing to my home folder works fine. I suspect this is your problem.

Addtionally, you're writing the file twice. The first time is showing an example of using the SerializeObject method to get a string back which is used with WriteAllText. The second block of code is using a StreamWriter. For your purposes, both are equivalent and you only need to use one or the other.

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

Comments

0
        using (StreamWriter file = File.CreateText(@"C:\users\user1.json"))
        {
            var jsonResult = JsonConvert.SerializeObject(i);
            file.WriteLine(jsonResult);
        }

1 Comment

newtonsoft.com/json/help/html/… You're rewriting the method being called.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.