/// <summary> /// Parse a generic embedded object /// </summary> /// <param name="content">Post content</param> /// <param name="type">EmbedType</param> /// <param name="provider">Provider</param> /// <param name="format">Function to parse matched content</param> internal static string Parse(string content, EmbedType type, Provider provider, Func <string, EmbedParsed, string> format) { // Get Embedded info const string pattern = "<!-- wp:embed ({.*?}) -->(.*?)<!-- /wp:embed -->"; var matches = Regex.Matches(content, pattern, RegexOptions.Singleline); if (matches.Count == 0) { return(content); } // Parse each match foreach (Match match in matches) { // Info is encoded as JSON so deserialise it first var info = match.Groups[1].Value; _ = JsonF.Deserialise <EmbedParsed>(info).IfSome(embed => { // Only replace matching embed types if (embed.Type == type && embed.ProviderNameSlug == provider) { // Replace content using child Format() method content = content.Replace( match.Value, format(Rnd.StringF.Get(10), embed) ); } }); } // Return parsed content return(content); }
public void ValidJson_ReturnsObject() { // Arrange var v0 = Rnd.Lng; var v1 = Rnd.Str; var v2 = Rnd.Int; var v3 = Rnd.DateTime; var v4 = Rnd.Flip; var input = "{" + $"\"id\":\"{v0}\"," + $"\"str\":\"{v1}\"," + $"\"num\":{v2}," + $"\"dt\":\"{v3:s}\"," + $"\"mbe\":{JsonF.Bool(v4)}," + "\"empty\":null" + "}"; var expected = new Test { Id = new(v0), Str = v1, Num = v2, DT = v3, Mbe = v4 }; // Act var result = JsonF.Deserialise <Test>(input).Unwrap(() => new Test()); // Assert Assert.Equal(expected, result, new TestComparer()); }
/// <summary> /// Parse WordPress photo gallery /// </summary> /// <param name="content">Post content</param> internal static string Parse(string content) { // Get Gallery info const string pattern = "<!-- wp:gallery ({.*?}) -->(.*?)<!-- /wp:gallery -->"; var matches = Regex.Matches(content, pattern, RegexOptions.Singleline); if (matches.Count == 0) { return(content); } // Replacement format const string format = "<div id=\"{0}\" class=\"hide image-gallery\" data-ids=\"{1}\" data-cols=\"{2}\"></div>"; // Parse each match foreach (Match match in matches) { // Info is encoded as JSON so deserialise it first var info = match.Groups[1].Value; _ = JsonF.Deserialise <GalleryParsed>(info).IfSome(gallery => content = content.Replace( match.Value, string.Format(CultureInfo.InvariantCulture, format, Rnd.StringF.Get(10), string.Join(",", gallery.Ids), gallery.Columns) ) ); } // Return parsed content return(content); }
/// <summary> /// Deserialise list from JSON /// </summary> /// <typeparam name="T">Enumerated value type</typeparam> /// <param name="json">JSON serialised list</param> public static EnumeratedList <T> Deserialise <T>(string json) where T : Enumerated { var strings = JsonF.Deserialise <List <string> >(json).Unwrap(() => new List <string>()); return(new EnumeratedList <T>(strings)); }
public void Null_Or_Whitespace_Returns_None(string input) { // Arrange // Act var result = JsonF.Deserialise <Test>(input); // Assert result.AssertNone().AssertType <DeserialisingNullOrEmptyStringMsg>(); }
public void Deserialise_Null_Or_Invalid_Value_Returns_None_With_DeserialisingValueExceptionMsg(string input) { // Arrange // Act var result = JsonF.Deserialise <Maybe <Test> >(input); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Deserialise_Object_With_Enumerated_Property_But_Null_Value_And_Disallow_Empty_Returns_None(string input) { // Arrange var json = $"{{ \"enumeratedValue\": {input} }}"; // Act var result = JsonF.Deserialise <EnumeratedWrapperTest1>(json); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void InvalidJson_Returns_None() { // Arrange var input = Rnd.Str; // Act var result = JsonF.Deserialise <Test>(input); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Deserialise_Object_With_Maybe_Property_Null_Or_Invalid_Value_Returns_None_With_DeserialisingValueExceptionMsg(string input) { // Arrange var json = $"{{\"test\":{input}}}"; // Act var result = JsonF.Deserialise <Wrapper>(json); // Assert result.AssertNone().AssertType <DeserialiseExceptionMsg>(); }
public void Deserialise_Null_Returns_Object_With_Empty_Value() { // Arrange var json = JsonF.Empty; // Act var result = JsonF.Deserialise <EnumeratedTest0>(json); // Assert var some = result.AssertSome(); Assert.Equal(string.Empty, some.ToString()); }
public void Deserialise_Returns_Object_With_Value() { // Arrange var value = Rnd.Str; var json = $"\"{value}\""; // Act var result = JsonF.Deserialise <EnumeratedTest0>(json); // Assert var some = result.AssertSome(); Assert.Equal(value, some.ToString()); }
public void Deserialise_Object_With_Enumerated_Property_Returns_Object() { // Arrange var id = Rnd.Int; var value = Rnd.Str; var json = $"{{ \"id\": {id}, \"enumeratedValue\": \"{value}\" }}"; // Act var result = JsonF.Deserialise <EnumeratedWrapperTest0>(json); // Assert var some = result.AssertSome(); Assert.Equal(id, some.Id); Assert.Equal(value, some.EnumeratedValue.ToString()); }
public void Deserialise_Returns_Object_With_Property() { // Arrange var valueStr = Rnd.Str; var valueInt = Rnd.Int; var json = $"{{\"test\":{{\"foo\":\"{valueStr}\",\"bar\":{valueInt}}}}}"; var expected = new Wrapper(new Test(valueStr, valueInt)); // Act var result = JsonF.Deserialise <Wrapper>(json); // Assert var some = result.AssertSome(); Assert.Equal(expected, some); }
/// <summary> /// Decrypt a string using the specified key /// </summary> /// <typeparam name="T">Type of object being encrypted</typeparam> /// <param name="this">JSON-serialised Box</param> /// <param name="key">Encryption Key</param> public static Maybe <T> Decrypt <T>(this string @this, string key) => from l in JsonF.Deserialise <Locked <T> >(@this) from c in l.Unlock(key) select c.Contents;