JackWFinlay.Jsonize 3.0.0-alpha.1

Suggested Alternatives

Jsonize

Additional Details

The "JackWFinlay.Jsonize" package has moved to the package "Jsonize". Please see project site for more details.

This is a prerelease version of JackWFinlay.Jsonize.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package JackWFinlay.Jsonize --version 3.0.0-alpha.1
NuGet\Install-Package JackWFinlay.Jsonize -Version 3.0.0-alpha.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JackWFinlay.Jsonize" Version="3.0.0-alpha.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JackWFinlay.Jsonize --version 3.0.0-alpha.1
#r "nuget: JackWFinlay.Jsonize, 3.0.0-alpha.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JackWFinlay.Jsonize as a Cake Addin
#addin nuget:?package=JackWFinlay.Jsonize&version=3.0.0-alpha.1&prerelease

// Install JackWFinlay.Jsonize as a Cake Tool
#tool nuget:?package=JackWFinlay.Jsonize&version=3.0.0-alpha.1&prerelease

Jsonize

Convert HTML to JSON with this .Net Standard 2.0 package.

Version 3.*.*

Version 3.0.0 introduces breaking changes to the Jsonize project. The project has been completely rewritten to decouple, simplify, and keep up with new standards.

The project now splits the parsing and serialization into separate areas of concern, as noted by the introduction of the IJsonizeParser and IJsonizeSerializer interfaces, found in the JackWFinlay.Jsonize.Abstractions package. These can be implemented by anyone, but a brand new parser has been written using AngleSharp as its HTML engine. This is supplied as the JackWFinlay.Jsonize.Parser.AngleSharp package. There is also the JackWFinlay.Jsonize.Serializer.NewtonsoftJson package, which implements a basic serializer wrapping the Newtonsoft.Json package with some useful functions. Feel free to implement your own serializer - there are loose plans to use the new System.Text.Json serializer as a new package.

The JackWFinlay.Jsonize package simply wraps the parser and serializer functions into one. Jsonize no longer will grab any content from the internet for you; you must supply the HTML as a string to Jsonizer class methods.

Usage

An example to get the site "https://jackfinlay.com" as a JSON string:

private static async Task<string> Testy(string q = "")
{
    using (var client = new HttpClient())
    {
        string url = @"https://jackfinlay.com";

        HttpResponseMessage response = await client.GetAsync(url);

        string html = await response.Content.ReadAsStringAsync();

        // The use of the parameterless constructors will use default settings.
        AngleSharpJsonizeParser parser = new AngleSharpJsonizeParser();
        NewtonsoftJsonJsonizeSerializer serializer = new NewtonsoftJsonJsonizeSerializer();        

        Jsonizer jsonizer = new Jsonize(parser, serializer);

        return jsonizer.ParseToStringAsync();
    }
}

Alternatively, get the response as a JsonizeNode:

return jsonizer.ParseToJsonizeNodeAsync();

You can control the output with a JsonizeParserConfiguration object, which is passed as a parameter to the constructor of the IJsonizeParser of choice:

JsonizeParserConfiguration parserConfiguration = new JsonizeParserConfiguration()
{
    NullValueHandling = NullValueHandling.Ignore,
    EmptyTextNodeHandling = EmptyTextNodeHandling.Ignore,
    TextTrimHandling = TextTrimHandling.Trim,
    ClassAttributeHandling = ClassAttributeHandling.Array
}

JsonizeConfiguration jsonizeConfiguration = new JsonizeConfiguration
{
    Parser = new AngleSharpJsonizeParser(parserConfiguration),
    Serializer = new NewtonsoftJsonJsonizeSerializer()
};

Jsonizer jsonizer = new Jsonizer(jsonizeConfiguration);

Results are in the form:

{
    "nodeType":"Node type e.g. Document, Element, or Comment",
    "tag":"If node is Element this will display the tag e.g p, h1 ,div etc.",
    "text":"If node is of type Text, this will display the text in that node.",
    "attr":{
                "name":"value",
                "class": []
            },
    "children":[
                {
                    "nodeType":"Node type e.g. Document, Element, or Comment",
                    "tag":"If node is Element this will display the tag e.g p, h1 ,div etc.",
                    "text":"If node is of type Text, this will display the text in that node.",
                    "child": []
                }
            ]
}

Example:

The following HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Jsonize</title>
    </head>
    <body>
        <div id="parent" class="parent-div">
            <div id="child1" class="child-div child1">Some Text</div>
            <div id="child2" class="child-div child2">Some Text</div>
            <div id="child3" class="child-div child3">Some Text</div>
        </div>
    </body>
</html>

Becomes:

{
    "nodeType": "Document",
    "tag": null,
    "text": null,
    "attr": {},
    "children": [
        {
            "nodeType": "DocumentType",
            "tag": "html",
            "text": null,
            "attr": {},
            "children": []
        },
        {
            "nodeType": "Element",
            "tag": "html",
            "text": null,
            "attr": {},
            "children": [
                {
                    "nodeType": "Element",
                    "tag": "head",
                    "text": null,
                    "attr": {},
                    "children": [
                        {
                            "nodeType": "Element",
                            "tag": "title",
                            "text": "Jsonize",
                            "attr": {},
                            "children": []
                        }
                    ]
                },
                {
                    "nodeType": "Element",
                    "tag": "body",
                    "text": null,
                    "attr": {},
                    "children": [
                        {
                            "nodeType": "Element",
                            "tag": "div",
                            "text": null,
                            "attr": {
                                "id": "parent",
                                "class": [
                                    "parent-div"
                                ]
                            },
                            "children": [
                                {
                                    "nodeType": "Element",
                                    "tag": "div",
                                    "text": "Some Text",
                                    "attr": {
                                        "id": "child1",
                                        "class": [
                                            "child-div",
                                            "child1"
                                        ]
                                    },
                                    "children": []
                                },
                                {
                                    "nodeType": "Element",
                                    "tag": "div",
                                    "text": "Some Text",
                                    "attr": {
                                        "id": "child2",
                                        "class": [
                                            "child-div",
                                            "child2"
                                        ]
                                    },
                                    "children": []
                                },
                                {
                                    "nodeType": "Element",
                                    "tag": "div",
                                    "text": "Some Text",
                                    "attr": {
                                        "id": "child3",
                                        "class": [
                                            "child-div",
                                            "child3"
                                        ]
                                    },
                                    "children": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 8,691 3/3/2017
1.0.8 1,295 10/21/2016
1.0.7 1,285 10/11/2016
1.0.6 1,256 10/9/2016
1.0.5 1,271 10/6/2016
1.0.4 1,358 10/4/2016
1.0.3 1,254 10/3/2016
1.0.2 1,098 10/2/2016
1.0.1 1,212 10/1/2016