aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/Identifier.cs
blob: 5724100798e4b198b0631fe6d197a3c8276fb26c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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.Diagnostics;
    using SimpleJson;

    /// <summary>
    /// Class to define the identifier and access for a symbol.
    /// </summary>
    [DebuggerDisplay("{Access} {Id,nq}")]
    public class Identifier
    {
        public static Identifier Invalid = new Identifier(AccessModifier.Private, (string)null);

        [Obsolete]
        public Identifier(string id, AccessModifier access)
        {
            this.Id = id;
            this.Access = access;
        }

        public Identifier(AccessModifier access, string id)
        {
            this.Access = access;
            this.Id = id;
        }

        public Identifier(AccessModifier access, params string[] ids)
        {
            this.Access = access;
            this.Id = String.Join("/", ids);
        }

        public Identifier(AccessModifier access, params object[] ids)
        {
            this.Access = access;
            this.Id = String.Join("/", ids);
        }

        public Identifier(AccessModifier access, int id)
        {
            this.Access = access;
            this.Id = id.ToString();
        }

        /// <summary>
        /// Access modifier for a symbol.
        /// </summary>
        public AccessModifier Access { get; }

        /// <summary>
        /// Identifier for the symbol.
        /// </summary>
        public string Id { get; }

        internal static Identifier Deserialize(JsonObject jsonObject)
        {
            var id = jsonObject.GetValueOrDefault<string>("id");
            var accessValue = jsonObject.GetValueOrDefault<string>("access");
            Enum.TryParse(accessValue, true, out AccessModifier access);

            return new Identifier(access, id);
        }

        internal JsonObject Serialize()
        {
            var jsonObject = new JsonObject
            {
                { "id", this.Id },
                { "access", this.Access.ToString().ToLowerInvariant() }
            };

            return jsonObject;
        }
    }
}