Coder Social home page Coder Social logo

x-oss-byte / .net-json-transformer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dday9/.net-json-transformer

0.0 0.0 1.0 60 KB

A Visual Basic .NET (VB.NET) implementation that transforms JSON Strings into a managed .NET XDocument

License: MIT License

Visual Basic .NET 100.00%

.net-json-transformer's Introduction

ref : [email protected]

.NET-JSON-Transformer

A Visual Basic .NET (VB.NET) implementation that converts a JSON literal into a .NET XDocument

Add to project

The json.vb code file is uncompiled. Follow these instructions to add the file to your project:

  1. Project > Add Existing Item (shift + alt + a)
  2. Select the file in the browse dialog
  3. Add

Json.Parse Method

Creates a new XDocument from a JSON literal

Public Shared Function Parse(ByVal source As String, Optional culuture As CultureInfo) As XDocument

Parameters

source String A string that contains JSON.

culture And optional parameter to specify the culture. This is used for culture specific parsing (e.g. commas used as decimal place)

Returns

XDocument An XDocument populated from the string that contains JSON.

Example

The following example illustrates the Json.Parse method:

Const literal = "{ ""Property1"": 1, ""Property2"": false }"
Dim parsedJson = Json.Parse(literal)
Console.WriteLine(parsedJson.ToString())

' <object>
'   <item>
'     <key>Property1</key>
'     <value>
'       <number>1</number>
'     </value>
'   </item>
'   <item>
'     <key>Property2</key>
'     <value>
'       <boolean>false</boolean>
'     </value>
'   </item>
' </object>

Remarks

  • The parser ignores whitespace, essentially minfying the JSON. For example, if the JSON literal is:
    {
        "glossary": {
            "title": "example glossary",
            "GlossDiv": {
                "title": "S",
                "GlossList": {
                    "GlossEntry": {
                        "ID": "SGML",
                        "SortAs": "SGML",
                        "GlossTerm": "Standard Generalized Markup Language",
                        "Acronym": "SGML",
                        "Abbrev": "ISO 8879:1986",
                        "GlossDef": {
                            "para": "A meta-markup language, used to create markup languages such as DocBook.",
                            "GlossSeeAlso": ["GML", "XML"]
                        },
                        "GlossSee": "markup"
                    }
                }
            }
        }
    }

Then it gets parsed as:

{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
  • The returned XML is a 1-to-1 translation from the JSON. Using the same example as above, the resulting XML would be:

    <object>
      <item>
        <key>glossary</key>
        <value>
          <object>
            <item>
              <key>title</key>
              <value>
                <string>example glossary</string>
              </value>
            </item>
            <item>
              <key>GlossDiv</key>
              <value>
                <object>
                  <item>
                    <key>title</key>
                    <value>
                      <string>S</string>
                    </value>
                  </item>
                  <item>
                    <key>GlossList</key>
                    <value>
                      <object>
                        <item>
                          <key>GlossEntry</key>
                          <value>
                            <object>
                              <item>
                                <key>ID</key>
                                <value>
                                  <string>SGML</string>
                                </value>
                              </item>
                              <item>
                                <key>SortAs</key>
                                <value>
                                  <string>SGML</string>
                                </value>
                              </item>
                              <item>
                                <key>GlossTerm</key>
                                <value>
                                  <string>Standard Generalized Markup Language</string>
                                </value>
                              </item>
                              <item>
                                <key>Acronym</key>
                                <value>
                                  <string>SGML</string>
                                </value>
                              </item>
                              <item>
                                <key>Abbrev</key>
                                <value>
                                  <string>ISO 8879:1986</string>
                                </value>
                              </item>
                              <item>
                                <key>GlossDef</key>
                                <value>
                                  <object>
                                    <item>
                                      <key>para</key>
                                      <value>
                                        <string>A meta-markup language, used to create markup languages such as DocBook.</string>
                                      </value>
                                    </item>
                                    <item>
                                      <key>GlossSeeAlso</key>
                                      <value>
                                        <array>
                                          <string>GML</string>
                                          <string>XML</string>
                                        </array>
                                      </value>
                                    </item>
                                  </object>
                                </value>
                              </item>
                              <item>
                                <key>GlossSee</key>
                                <value>
                                  <string>markup</string>
                                </value>
                              </item>
                            </object>
                          </value>
                        </item>
                      </object>
                    </value>
                  </item>
                </object>
              </value>
            </item>
          </object>
        </value>
      </item>
    </object>
    
  • The parser does not parse to the exact specifications of the EBNF found on http://www.json.org/ the following list the deviations in this parser:

    • Number: Checks if the number starts with either a positive sign or negative sign
    • Boolean: Checks for "true" or "false" based on case-insensitivity
    • Null: Checks for "null" based on case-insensitivity
    • String: Does not check for "\u" followed by 4 hexadecimal characters as an escape character

Examples and Demo

The following example demonstrates the Parse method.

Public Module Module1

  Public Sub Main()
      Const literal = "{
                          ""glossary"": {
                              ""title"": ""example glossary"",
  	                        ""GlossDiv"": {
                                  ""title"": ""S"",
  		                        ""GlossList"": {
                                      ""GlossEntry"": {
                                          ""ID"": ""SGML"",
  				                        ""SortAs"": ""SGML"",
  				                        ""GlossTerm"": ""Standard Generalized Markup Language"",
  				                        ""Acronym"": ""SGML"",
  				                        ""Abbrev"": ""ISO 8879:1986"",
  				                        ""GlossDef"": {
                                              ""para"": ""A meta-markup language, used to create markup languages such as DocBook."",
  					                        ""GlossSeeAlso"": [""GML"", ""XML""]
                                          },
  				                        ""GlossSee"": ""markup""
                                      }
                                  }
                              }
                          }
                      }"
      Dim parsedJson = Json.Parse(literal)
      Console.WriteLine(parsedJson.ToString())
      Console.ReadLine()
  End Sub

End Module

Fiddle: https://dotnetfiddle.net/FqqPnW

Donate

Show your support! Your (non-tax deductible) donation of Monero cryptocurrency is a sign of solidarity among web developers.

Being self taught, I have come a long way over the years. I certainly do not intend on making a living from this free feature, but my hope is to earn a few dollars to validate all of my hard work.

Monero Address: 447SPi8XcexZnF7kYGDboKB6mghWQzRfyScCgDP2r4f2JJTfLGeVcFpKEBT9jazYuW2YG4qn51oLwXpQJ3oEXkeXUsd6TCF

447SPi8XcexZnF7kYGDboKB6mghWQzRfyScCgDP2r4f2JJTfLGeVcFpKEBT9jazYuW2YG4qn51oLwXpQJ3oEXkeXUsd6TCF

.net-json-transformer's People

Contributors

dday9 avatar x-oss-byte avatar

Forkers

renovate-bot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.