AvalonEdit - xshd for JSON Highlighting

Is there an xshd rule set for an AvalonEdit control to highlight JSON syntax? I tried the definition for JavaScript, but it does not work, i.e.:

{
   "name" : "value"
}

both names and values ​​have the same color using the JavaScript definition.

Is there a rule set for JSON, and if not, how can I change xshd to get different colors for the name and value in JSON?

+4
source share
2 answers

If someone needs something like this, I worked like this:

<?xml version="1.0" encoding="utf-8" ?>
<SyntaxDefinition name="Json" extensions=".js" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
  <Color name="Digits" foreground="#8700FF" exampleText="3.14" />
  <Color name="Value" foreground="#000CFF" exampleText="var text = &quot;Hello, World!&quot;;" />
  <Color name="ParamName" foreground="#057500"  exampleText="var text = &quot;Hello, World!&quot;;" />
  <RuleSet ignoreCase="false">
    <Keywords color="Digits" >
      <Word>true</Word>
      <Word>false</Word>
    </Keywords>
    <Span color="ParamName">
      <Begin>"</Begin>
      <End>(?=:)</End>
    </Span>
    <Span color="Value" multiline="true">
      <Begin>
        (?&lt;=:)\040"[^"]*
      </Begin>
      <End>"</End>
    </Span>
    <Rule color="Digits">\b0[xX][0-9a-fA-F]+|(\b\d+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?</Rule>
  </RuleSet>
</SyntaxDefinition>

Not perfect, but enough for me.

+5
source

Just use AvalonEdit JavaScript highlighter .

Code example:

using (var stream = Assembly.GetAssembly(typeof(ICSharpCode.AvalonEdit.TextEditor)).GetManifestResourceStream("ICSharpCode.AvalonEdit.Highlighting.Resources.JavaScript-Mode.xshd"))
{
    using (var reader = new XmlTextReader(stream))
    {
        avalonEdit.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
        SearchPanel.Install(avalonEdit);
    }
}
-1
source

Source: https://habr.com/ru/post/1612860/


All Articles