• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 960

15 May 2026 07:18AM UTC coverage: 0.0% (-94.1%) from 94.053%
960

Pull #411

appveyor

web-flow
Merge ef6b054ce into 4cec191ae
Pull Request #411: Bump MSTest.TestFramework from 4.2.2 to 4.2.3

0 of 2438 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ConsoleApplication/JsonFormatter.cs
1
// <copyright file="JsonFormatter.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Collections.Generic;
8
using System.IO;
9
using System.Text.Json;
10

11
/// <summary>
12
/// Renders action results as JSON.
13
/// </summary>
14
public class JsonFormatter : IFormatter
15
{
16
    private readonly TextWriter _textWriter;
17
    private Utf8JsonWriter _json = null!;
×
18

19
    /// <summary>
20
    /// Initializes a new instance of the <see cref="JsonFormatter"/> class.
21
    /// </summary>
22
    public JsonFormatter(TextWriter textWriter)
×
23
    {
×
24
        _textWriter = textWriter;
×
25
    }
×
26

27
    /// <inheritdoc/>
28
    public void Write(ActionOutput output, ProgramContext ac)
29
    {
×
30
        using var stream = new MemoryStream();
×
31
        using var json = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
×
32
        _json = json;
×
33

34
        json.WriteStartArray();
×
35
        output.WriteTo(this, ac);
×
36
        json.WriteEndArray();
×
37
        json.Flush();
×
38

39
        _textWriter.Write(System.Text.Encoding.UTF8.GetString(stream.ToArray()));
×
40
        _textWriter.WriteLine();
×
41
    }
×
42

43
    /// <inheritdoc/>
44
    public void Write(ActionOutput.Networks output, ProgramContext ac)
45
    {
×
46
        WriteNetworks(output.Items, ac);
×
47
    }
×
48

49
    /// <inheritdoc/>
50
    public void Write(ActionOutput.NetworkGroups output, ProgramContext ac)
51
    {
×
52
        for (int i = 0; i < output.Groups.Count; i++)
×
53
        {
×
54
            var group = output.Groups[i];
×
55
            if (group.Count == 0)
×
56
            {
×
57
                _json.WriteStartObject();
×
58
                _json.WriteString("error",
×
59
                    $"Unable to subnet ipnetwork {output.InputNetworks[i]} into cidr {output.SubnetCidr}");
×
60
                _json.WriteEndObject();
×
61
            }
×
62
            else
63
            {
×
64
                _json.WriteStartArray();
×
65
                WriteNetworks(group, ac);
×
66
                _json.WriteEndArray();
×
67
            }
×
68
        }
×
69
    }
×
70

71
    /// <inheritdoc/>
72
    public void Write(ActionOutput.SubtractResults output, ProgramContext ac)
73
    {
×
74
        WriteNetworks(output.Items, ac);
×
75
    }
×
76

77
    /// <inheritdoc/>
78
    public void Write(ActionOutput.ContainResults output, ProgramContext ac)
79
    {
×
80
        foreach (var r in output.Items)
×
81
        {
×
82
            _json.WriteStartObject();
×
83
            _json.WriteString("network", r.Network);
×
84
            _json.WriteString("test", r.Test);
×
85
            _json.WriteBoolean("contains", r.Contains);
×
86
            _json.WriteEndObject();
×
87
        }
×
88
    }
×
89

90
    /// <inheritdoc/>
91
    public void Write(ActionOutput.OverlapResults output, ProgramContext ac)
92
    {
×
93
        foreach (var r in output.Items)
×
94
        {
×
95
            _json.WriteStartObject();
×
96
            _json.WriteString("network", r.Network);
×
97
            _json.WriteString("test", r.Test);
×
98
            _json.WriteBoolean("overlaps", r.Overlaps);
×
99
            _json.WriteEndObject();
×
100
        }
×
101
    }
×
102

103
    /// <inheritdoc/>
104
    public void Write(ActionOutput.IpAddresses output, ProgramContext ac)
105
    {
×
106
        foreach (string addr in output.Items)
×
107
        {
×
108
            _json.WriteStringValue(addr);
×
109
        }
×
110
    }
×
111

112
    /// <inheritdoc/>
113
    public void Write(ActionOutput.Error output, ProgramContext ac)
114
    {
×
115
        _json.WriteStartObject();
×
116
        _json.WriteString("error", output.Message);
×
117
        _json.WriteEndObject();
×
118
    }
×
119

120
    /// <inheritdoc/>
121
    public void Write(ActionOutput.UsageInfo output, ProgramContext ac)
122
    {
×
123
        if (output.Errors.Count > 0)
×
124
        {
×
125
            foreach (string error in output.Errors)
×
126
            {
×
127
                _json.WriteStartObject();
×
128
                _json.WriteString("error", error);
×
129
                _json.WriteEndObject();
×
130
            }
×
131

132
            return;
×
133
        }
134

135
        _json.WriteStartObject();
×
136
        _json.WriteString("version", output.Version);
×
137
        _json.WriteString("synopsis", output.Synopsis);
×
138

139
        _json.WritePropertyName("optionGroups");
×
140
        _json.WriteStartArray();
×
141
        foreach (var group in output.OptionGroups)
×
142
        {
×
143
            _json.WriteStartObject();
×
144
            _json.WriteString("name", group.Name);
×
145
            _json.WritePropertyName("options");
×
146
            _json.WriteStartArray();
×
147
            foreach (var opt in group.Options)
×
148
            {
×
149
                _json.WriteStartObject();
×
150
                _json.WriteString("flag", opt.Flag);
×
151
                if (opt.ArgName is not null)
×
152
                {
×
153
                    _json.WriteString("argName", opt.ArgName);
×
154
                }
×
155

156
                _json.WriteString("description", opt.Description);
×
157
                if (opt.Example is not null)
×
158
                {
×
159
                    _json.WriteString("example", opt.Example);
×
160
                }
×
161

162
                _json.WriteEndObject();
×
163
            }
×
164

165
            _json.WriteEndArray();
×
166
            _json.WriteEndObject();
×
167
        }
×
168

169
        _json.WriteEndArray();
×
170

171
        _json.WritePropertyName("positionalArgs");
×
172
        _json.WriteStartObject();
×
173
        _json.WriteString("name", "networks");
×
174
        _json.WriteString("description", output.NetworksDescription);
×
175
        _json.WritePropertyName("examples");
×
176
        _json.WriteStartArray();
×
177
        foreach (string example in output.NetworksExamples)
×
178
        {
×
179
            _json.WriteStringValue(example);
×
180
        }
×
181

182
        _json.WriteEndArray();
×
183
        _json.WriteEndObject();
×
184

185
        _json.WriteEndObject();
×
186
    }
×
187

188
    private void WriteNetworks(List<IPNetwork2> networks, ProgramContext ac)
189
    {
×
190
        foreach (var ipn in networks)
×
191
        {
×
192
            WriteNetwork(ipn, ac);
×
193
        }
×
194
    }
×
195

196
    private void WriteNetwork(IPNetwork2 ipn, ProgramContext ac)
197
    {
×
198
        _json.WriteStartObject();
×
199
        if (ac.IPNetwork) { _json.WriteString("ipnetwork", ipn.ToString()); }
×
200
        if (ac.Network) { _json.WriteString("network", ipn.Network?.ToString()); }
×
201
        if (ac.Netmask) { _json.WriteString("netmask", ipn.Netmask?.ToString()); }
×
202
        if (ac.Cidr) { _json.WriteNumber("cidr", ipn.Cidr); }
×
203

204
        if (ac.Broadcast)
×
205
        {
×
206
            if (ipn.Broadcast is not null)
×
207
            {
×
208
                _json.WriteString("broadcast", ipn.Broadcast.ToString());
×
209
            }
×
210
            else
211
            {
×
212
                _json.WriteNull("broadcast");
×
213
            }
×
214
        }
×
215

216
        if (ac.FirstUsable) { _json.WriteString("firstUsable", ipn.FirstUsable?.ToString()); }
×
217
        if (ac.LastUsable) { _json.WriteString("lastUsable", ipn.LastUsable?.ToString()); }
×
218
        if (ac.Usable) { _json.WriteString("usable", ipn.Usable.ToString()); }
×
219
        if (ac.Total) { _json.WriteString("total", ipn.Total.ToString()); }
×
220
        _json.WriteEndObject();
×
221
    }
×
222
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc