Nick
Nick

Reputation: 25

JSON file as database

I'm tring to use a JSON file as database for static content in ASP.net (an image gallery). Everything works well: I can read the file and retrieve data and display them in a easy way. I'm currently using JSON.net and c# serverside.

This is part of my JSON (hand made):

{
    "Category 1": [
        {
            "TitleFile": "Title of IMG1",
            "FileName": "IMG1",
            "URLThumbFile": "Content/images/Gallery/IMG1_tb.png",
            "URLFile": "Content/images/Gallery/IMG1.png",
            "DescFile": "DESC IMG1"
        },
        {
            "TitleFile": "Title of IMG2",
            "FileName": "IMG2",
            "URLThumbFile": "Content/images/Gallery/IMG2_tb.png",
            "URLFile": "Content/images/Gallery/IMG2.png",
            "DescFile": "DESC IMG2"

        }
    ],
    "Category 2": [
        {
            "TitleFile": "Title of IMG3",
            "FileName": "IMG3",
            "URLThumbFile": "Content/images/Gallery/IMG3_tb.png",
            "URLFile": "Content/images/Gallery/IMG3.png",
            "DescFile": "DESC IMG3"
        },
        {
            "TitleFile": "Title of IMG4",
            "FileName": "IMG4",
            "URLThumbFile": "Content/images/Gallery/IMG4_tb.png",
            "URLFile": "Content/images/Gallery/IMG4.png",
            "DescFile": "DESC IMG4"
        }
    ],
    "Category 3": [
        {
            "TitleFile": "Title of IMG5",
            "FileName": "IMG5",
            "URLThumbFile": "Content/images/Gallery/IMG5_tb.png",
            "URLFile": "Content/images/Gallery/IMG5.png",
            "DescFile": "DESC IMG5"
        }
    ]
}

Now, I was figuring how to create a sort of "upload form", in order to write this JSON file and store infos. Client side, everything is ok:

<asp:TextBox runat="server" ID="txtFileName" />
<asp:TextBox runat="server" ID="txtFileTitle" />
<asp:TextBox runat="server" ID="txtFileDesc" />
<asp:FileUpload runat="server" ID="UPLFileImg" />
<asp:DropDownList runat="server" ID="ddlCategory"/>
<asp:Button runat="server" ID="btnUploadFile" Text="Upload" OnClick="btnUploadFile_Click" />

C# side:

//Classes
public class JSONGalleryCategory //is it correct??
{
    public List<JSONGalleryContent> Category { get; set; }

}

public class JSONGalleryContent //??
{
    public string FileTitle { get; set; }
    public string FileName { get; set; }
    public string URLThumbFile { get; set; }
    public string URLFile { get; set; }
    public string DescFile { get; set; }
    public DateTime DataFile { get; set; }
}

//in the .aspx.cs
private void PopulateDDL()
{
    List<string> items = new List<string>();
    dynamic jsonFile = JObject.Parse(Utilities.ReadFile("~/Content/images/Gallery/Gallery.json"));
    foreach (JProperty category in jsonFile)
    {
        items.Add(category.Name);
    }
    ddlCategory.DataSource = items;
    ddlCategory.DataBind();
}

protected void btnUploadFile_Click(object sender, EventArgs e)
{           
    if (UPLFileImg.HasFile)
    {
        string fileName = Path.Combine(Server.MapPath("~/Content/images/Gallery/"), UPLFileImg.FileName);
        UPLFileImg.SaveAs(fileName);
        //Open Jsonfile...read content...deserialize?
        dynamic jsonFile = JObject.Parse(Utilities.ReadFile("~/Content/images/Gallery/Gallery.json"));
        //Create new node?
        //???

        //append new node?

        //serialize again and save...? ordering nodes??
        Utilities.WriteFile("~/Content/images/Gallery/Gallery.json", JsonConvert.SerializeObject("???", Formatting.Indented));
        Utilities.ShowMessage("Success");
    }
    else
    {
        Utilities.ShowMessage("Error");
    }
}

How can I obtain it?

Thanks in advance!

Upvotes: 0

Views: 12799

Answers (2)

Eldar Zeynalov
Eldar Zeynalov

Reputation: 409

An old question but if you want to use a file as a json database you can check this link. It is a professional and easy-to-use library

Upvotes: 3

Stephen Brickner
Stephen Brickner

Reputation: 2602

 dynamic jsonFile = JObject.Parse(Utilities.ReadFile("~/Content/images/Gallery/Gallery.json"));

using (FileStream fs = File.Open(@"~/Content/images/Gallery/Gallery.json", FileMode.Open)) //may have to server.mappath this
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
    jw.Formatting = Formatting.Indented;

    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(jw, jsonFile);
}

Upvotes: 0

Related Questions