Tuesday, February 16, 2016

C# formatting JSON as rtf document (can be useful to display on Richtext controls or exporting rtf files)

C# code that converts JSON string to RTF


sInJSON = sInJSON.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace(Environment.NewLine, "\\line" + Environment.NewLine);
string sRTFOut = @"{\rtf1\ansi\deff0
{\colortbl;\red0\green0\blue0;\red255\green140\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green0\blue255;\red0\green0\blue128;\red0\green128\blue128}" +
    Regex.Replace(
        sInJSON,
        @"(¤(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\¤])*¤(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)".Replace('¤', '"'),
        match =>
        {
            var cls = "\\cf2";
            if (Regex.IsMatch(match.Value, @"^¤".Replace('¤', '"')))
            {
                if (Regex.IsMatch(match.Value, ":$"))
                {
                    cls = "\\cf3";
                }
                else {
                    cls = "\\cf4";
                }
            }
            else if (Regex.IsMatch(match.Value, "true|false"))
            {
                cls = "\\cf5";
            }
            else if (Regex.IsMatch(match.Value, "null"))
            {
                cls = "\\cf6";
            }
            return cls + match + "\\cf1";
        })
+ "}";


Please review the RTF code outputted down below.

Points to Note: The RTF file always start with {\rtf1\ansi\deff0
After this, you can declare your color palatte as: {\colortbl;\red0\green0\blue0;\red255\green140\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green0\blue255;\red0\green0\blue128;\red0\green128\blue128}
and each of these can be later referred to as \cf1, \cf2 etc
Please also note that chars \, { and } are considered special characters. If you have it in your text, please override with \. For example, } should be changed as \} to display }

Example RTF code output:

{\rtf1\ansi\deff0
{\colortbl;\red0\green0\blue0;\red255\green140\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green0\blue255;\red0\green0\blue128;\red0\green128\blue128}\{\line
    \cf3"firstName":\cf1 \cf4"John"\cf1,\line
    \cf3"lastName":\cf1 \cf4"Smith"\cf1,\line
    \cf3"isAlive":\cf1 \cf5true\cf1,\line
    \cf3"age":\cf1 \cf225\cf1,\line
    \cf3"address":\cf1\line
    \{\line
        \cf3"streetAddress":\cf1 \cf4"21 2nd Street"\cf1,\line
        \cf3"city":\cf1 \cf4"New York"\cf1,\line
        \cf3"state":\cf1 \cf4"NY"\cf1,\line
        \cf3"postalCode":\cf1 \cf4"10021-3100"\cf1\line
    \},\line
    \cf3"phoneNumbers":\cf1\line
    [\line
        \{\line
            \cf3"type":\cf1 \cf4"home"\cf1,\line
            \cf3"number":\cf1 \cf4"212 555-1234"\cf1\line
        \},\line
        \{\line
            \cf3"type":\cf1 \cf4"office"\cf1,\line
            \cf3"number":\cf1 \cf4"646 555-4567"\cf1\line
        \}\line
    ],\line
    \cf3"children":\cf1\line
    [\line
    ],\line
    \cf3"spouse":\cf1 \cf6null\cf1\line
\}\line
}

Displayed as RTF:

{
   "firstName":"John",
  
"lastName":"Smith",
  
"isAlive":true,
  
"age":,
  
"address":
   {
      
"streetAddress":"21 2nd Street",
      
"city":"New York",
      
"state":"NY",
      
"postalCode":"10021-3100"
   },
  
"phoneNumbers":
   [
       {
          
"type":"home",
          
"number":"212 555-1234"
       },
       {
          
"type":"office",
          
"number":"646 555-4567"
       }
   ],
  
"children":
   [
   ],
 

You can also use this method to save/export to RTF files using your winform or ASP.Net web application

1 comment:

AJ said...

The above code has a slight flaw, which I know the fix for. The issue is that the numbers are not being displayed (see age: in the above example).

The fix is to change the line:

var cls = "\\cf2";

to

var cls = "\\cf2 ";

The space at the end is the fix, as numbers now get displayed correctly.

Turn on Windows 11 Fast Boot

If windows starting is slow, to enable windows 11 fast startup/boot,  Press Windows + R, type powercfg.cpl, and hit Enter.  This will direct...