Example #1
0
    private bool ReadJson()
    {
        try
        {
            // Read the text from the sidecar file
            using StreamReader streamReader = SidecarFileInfo.OpenText();
            string json = streamReader.ReadToEnd();
            streamReader.Close();

            // Create object from text
            SidecarJson = SidecarFileJsonSchema.FromJson(json);
        }
        catch (Exception e) when(Log.Logger.LogAndHandle(e, MethodBase.GetCurrentMethod()?.Name))
        {
            return(false);
        }

        // Compare the schema version
        if (SidecarJson.SchemaVersion != SidecarFileJsonSchema.CurrentSchemaVersion)
        {
            Log.Logger.Warning("Sidecar JSON schema mismatch : {JsonSchemaVersion} != {CurrentSchemaVersion}, {FileName}",
                               SidecarJson.SchemaVersion,
                               SidecarFileJsonSchema.CurrentSchemaVersion,
                               SidecarFileInfo.Name);

            // Upgrade schema
            if (!SidecarFileJsonSchema.Upgrade(SidecarJson))
            {
                return(false);
            }
        }

        return(true);
    }
Example #2
0
    private bool WriteJson()
    {
        try
        {
            // Get json text from object
            string json = SidecarFileJsonSchema.ToJson(SidecarJson);

            // Write the text to the sidecar file
            using StreamWriter streamWriter = SidecarFileInfo.CreateText();
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
        catch (Exception e) when(Log.Logger.LogAndHandle(e, MethodBase.GetCurrentMethod()?.Name))
        {
            return(false);
        }
        return(true);
    }
 public static string ToJson(SidecarFileJsonSchema json)
 {
     return(JsonConvert.SerializeObject(json, Settings));
 }
 public static void ToFile(string path, SidecarFileJsonSchema json)
 {
     File.WriteAllText(path, ToJson(json));
 }