// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Data
{
using System;
using System.Collections.Generic;
using SimpleJson;
///
/// Section in an intermediate file.
///
public class IntermediateSection
{
///
/// Creates a new section as part of an intermediate.
///
/// Identifier for section.
/// Type of section.
/// Codepage for resulting database.
public IntermediateSection(string id, SectionType type, int codepage)
{
this.Id = id;
this.Type = type;
this.Codepage = codepage;
this.Tuples = new List();
}
///
/// Gets the identifier for the section.
///
/// Section identifier.
public string Id { get; }
///
/// Gets the type of the section.
///
/// Type of section.
public SectionType Type { get; }
///
/// Gets the codepage for the section.
///
/// Codepage for the section.
public int Codepage { get; set; }
///
/// Gets and sets the identifier of the compilation of the source file containing the section.
///
public string CompilationId { get; set; }
///
/// Gets and sets the identifier of the library that combined the section.
///
public string LibraryId { get; set; }
///
/// Tuples in the section.
///
public IList Tuples { get; }
///
/// Parse a section from the JSON data.
///
internal static IntermediateSection Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject)
{
var codepage = jsonObject.GetValueOrDefault("codepage", 0);
var id = jsonObject.GetValueOrDefault("id");
var type = jsonObject.GetEnumOrDefault("type", SectionType.Unknown);
if (SectionType.Unknown == type)
{
throw new ArgumentException("JSON object is not a valid section, unknown section type", nameof(type));
}
var section = new IntermediateSection(id, type, codepage);
var tuplesJson = jsonObject.GetValueOrDefault("tuples");
foreach (JsonObject tupleJson in tuplesJson)
{
var tuple = IntermediateTuple.Deserialize(creator, baseUri, tupleJson);
section.Tuples.Add(tuple);
}
return section;
}
internal JsonObject Serialize()
{
var jsonObject = new JsonObject
{
{ "type", this.Type.ToString().ToLowerInvariant() },
{ "codepage", this.Codepage }
};
if (!String.IsNullOrEmpty(this.Id))
{
jsonObject.Add("id", this.Id);
}
var tuplesJson = new JsonArray(this.Tuples.Count);
foreach (var tuple in this.Tuples)
{
var tupleJson = tuple.Serialize();
tuplesJson.Add(tupleJson);
}
jsonObject.Add("tuples", tuplesJson);
return jsonObject;
}
}
}