Updated to .NET Framework 4.7.2 due to platform limitations, added email cc, bcc and some fixes.
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
namespace BizTalkMonitorConfigGenerator.Serialization
|
||||
{
|
||||
public static class JsonFileWriter
|
||||
{
|
||||
public static void WritePrettyJsonAtomic(string path, object value, bool overwriteExisting)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
throw new ArgumentException("Pfad ist leer.", nameof(path));
|
||||
}
|
||||
|
||||
var fullPath = Path.GetFullPath(path);
|
||||
var directory = Path.GetDirectoryName(fullPath);
|
||||
if (string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
throw new InvalidOperationException("Aus dem Zielpfad konnte kein Verzeichnis aufgelöst werden: " + path);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(directory);
|
||||
if (File.Exists(fullPath) && !overwriteExisting)
|
||||
{
|
||||
throw new IOException("Zieldatei existiert bereits und OverwriteExisting=false: " + fullPath);
|
||||
}
|
||||
|
||||
var serializer = new JavaScriptSerializer();
|
||||
serializer.MaxJsonLength = int.MaxValue;
|
||||
var compact = serializer.Serialize(value);
|
||||
var pretty = PrettyPrint(compact);
|
||||
|
||||
var tempPath = fullPath + ".tmp";
|
||||
File.WriteAllText(tempPath, pretty, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
|
||||
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
File.Copy(fullPath, fullPath + ".bak", true);
|
||||
File.Delete(fullPath);
|
||||
}
|
||||
|
||||
File.Move(tempPath, fullPath);
|
||||
}
|
||||
|
||||
private static string PrettyPrint(string json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var indent = 0;
|
||||
var quoted = false;
|
||||
|
||||
for (var i = 0; i < json.Length; i++)
|
||||
{
|
||||
var ch = json[i];
|
||||
|
||||
switch (ch)
|
||||
{
|
||||
case '{':
|
||||
case '[':
|
||||
sb.Append(ch);
|
||||
if (!quoted)
|
||||
{
|
||||
sb.AppendLine();
|
||||
indent++;
|
||||
AppendIndent(sb, indent);
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
case ']':
|
||||
if (!quoted)
|
||||
{
|
||||
sb.AppendLine();
|
||||
indent--;
|
||||
AppendIndent(sb, indent);
|
||||
sb.Append(ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(ch);
|
||||
}
|
||||
break;
|
||||
case '"':
|
||||
sb.Append(ch);
|
||||
if (i > 0 && json[i - 1] == '\\')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
quoted = !quoted;
|
||||
break;
|
||||
case ',':
|
||||
sb.Append(ch);
|
||||
if (!quoted)
|
||||
{
|
||||
sb.AppendLine();
|
||||
AppendIndent(sb, indent);
|
||||
}
|
||||
break;
|
||||
case ':':
|
||||
if (quoted)
|
||||
{
|
||||
sb.Append(ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(": ");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (!quoted && char.IsWhiteSpace(ch))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
sb.Append(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static void AppendIndent(StringBuilder sb, int indent)
|
||||
{
|
||||
for (var i = 0; i < indent; i++)
|
||||
{
|
||||
sb.Append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user