-
-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use object for Top / Left / Bottom / Right in MarginOptions (#2788)
* Use object for Top / Left / Bottom / Right in MarginOptions This is similar to the object Width / Height in PdfOptions and all of them already got converted by ConvertPrintParameterToInches * Add back PdfOptionsShouldWorkWithMarginWithNoUnits test * Expand comment for PrimitiveTypeConverter * Turn test into unit test * bump System.Text.Json * chrome 129.0.6668.100 --------- Co-authored-by: Darío Kondratiuk <[email protected]>
- Loading branch information
Showing
8 changed files
with
134 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PuppeteerSharp.Helpers.Json | ||
{ | ||
/// <summary> | ||
/// Support types (<see cref="decimal"/>, <see cref="int"/> and <see cref="string"/>) | ||
/// used by <see cref="PdfOptions"/> and <see cref="Media.MarginOptions"/> for serialization / deserialization. | ||
/// For usecases like <see href="https://github.com/hardkoded/puppeteer-sharp/issues/1001"/>. | ||
/// </summary> | ||
internal sealed class PrimitiveTypeConverter : JsonConverter<object> | ||
{ | ||
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return null; | ||
} | ||
else if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
return reader.GetString(); | ||
} | ||
else if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
if (reader.TryGetInt32(out var i)) | ||
{ | ||
return i; | ||
} | ||
else if (reader.TryGetDecimal(out var dec)) | ||
{ | ||
return dec; | ||
} | ||
} | ||
|
||
return JsonSerializer.Deserialize(ref reader, typeToConvert, options); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) | ||
{ | ||
if (value is null) | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
else if (value is string str) | ||
{ | ||
writer.WriteStringValue(str); | ||
} | ||
else if (value is decimal dec) | ||
{ | ||
writer.WriteNumberValue(dec); | ||
} | ||
else if (value is int i) | ||
{ | ||
writer.WriteNumberValue(i); | ||
} | ||
else | ||
{ | ||
JsonSerializer.Serialize(writer, value, options); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters