This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
/
GenericPropertiesPipeBind.cs
67 lines (54 loc) · 1.87 KB
/
GenericPropertiesPipeBind.cs
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
using Microsoft.SharePoint.Client;
using Newtonsoft.Json.Linq;
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.Commands.ClientSidePages;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace SharePointPnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class PropertyBagPipeBind
{
private readonly Hashtable _hashtable;
private string _jsonString;
private JObject _jsonObject;
public PropertyBagPipeBind(Hashtable hashtable)
{
_hashtable = hashtable;
_jsonString = null;
_jsonObject = null;
}
public PropertyBagPipeBind(string json)
{
_hashtable = null;
_jsonString = json;
_jsonObject = JObject.Parse(json);
}
public string Json => _jsonString;
public JObject JsonObject => _jsonObject ?? HashtableToJsonObject(_hashtable);
public Hashtable Properties => _hashtable;
public override string ToString() => Json ?? HashtableToJsonString(_hashtable);
private string HashtableToJsonString(Hashtable hashtable)
{
return HashtableToJsonObject(hashtable).ToString();
}
private JObject HashtableToJsonObject(Hashtable hashtable)
{
var obj = new JObject();
foreach (var key in hashtable.Keys)
{
var rawValue = hashtable[key];
// To ensure the value is not serialized as PSObject
object value = rawValue is PSObject
? ((PSObject)rawValue).BaseObject
: rawValue;
obj[key] = JToken.FromObject(value);
}
return obj;
}
}
}