// 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.Symbols = 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; }
///
/// Symbols in the section.
///
public IList Symbols { get; }
///
/// Parse a section from the JSON data.
///
internal static IntermediateSection Deserialize(ISymbolDefinitionCreator 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 symbolsJson = jsonObject.GetValueOrDefault("symbols");
foreach (JsonObject symbolJson in symbolsJson)
{
var symbol = IntermediateSymbol.Deserialize(creator, baseUri, symbolJson);
section.Symbols.Add(symbol);
}
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 symbolsJson = new JsonArray(this.Symbols.Count);
foreach (var symbol in this.Symbols)
{
var symbolJson = symbol.Serialize();
symbolsJson.Add(symbolJson);
}
jsonObject.Add("symbols", symbolsJson);
return jsonObject;
}
}
}