JsonFormDataSetVisitor
using AngleSharp.Dom.Io;
using AngleSharp.Extensions;
using AngleSharp.Html.Submitters.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace AngleSharp.Html.Submitters
{
internal sealed class JsonFormDataSetVisitor : IFormSubmitter, IFormDataSetVisitor
{
private readonly JsonObject _context;
public JsonFormDataSetVisitor()
{
_context = new JsonObject();
}
public void Text(FormDataSetEntry entry, string value)
{
JsonValue value2 = CreateValue(entry.Type, value);
IEnumerable<JsonStep> enumerable = JsonStep.Parse(entry.Name);
JsonElement context = _context;
foreach (JsonStep item in enumerable) {
context = item.Run(context, value2, false);
}
}
public void File(FormDataSetEntry entry, string fileName, string contentType, IFile file)
{
JsonElement context = _context;
Stream obj = (file != null && file.Body != null && file.Type != null) ? file.Body : Stream.Null;
MemoryStream memoryStream = new MemoryStream();
obj.CopyTo(memoryStream);
byte[] inArray = memoryStream.ToArray();
IEnumerable<JsonStep> enumerable = JsonStep.Parse(entry.Name);
JsonObject value = new JsonObject {
[AttributeNames.Type] = new JsonValue(contentType),
[AttributeNames.Name] = new JsonValue(fileName),
[AttributeNames.Body] = new JsonValue(Convert.ToBase64String(inArray))
};
foreach (JsonStep item in enumerable) {
context = item.Run(context, value, true);
}
}
public void Serialize(StreamWriter stream)
{
string value = _context.ToString();
stream.Write(value);
}
private static JsonValue CreateValue(string type, string value)
{
if (type.Is(InputTypeNames.Checkbox))
return new JsonValue(value.Is(Keywords.On));
if (type.Is(InputTypeNames.Number))
return new JsonValue(value.ToDouble(0));
return new JsonValue(value);
}
}
}