-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ node_modules/ | |
README.md | ||
dotnet/obj/ | ||
dotnet/bin/ | ||
dotnet-wrapper/ | ||
dotnet-wrapper/ | ||
.vs |
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,72 @@ | ||
namespace ReviewsRatings.Utils | ||
{ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using ReviewsRatings.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
class ReviewJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return (objectType == typeof(Review)); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
JObject jo = JObject.Load(reader); | ||
Review review = new Review(); | ||
if (jo["Approved"] != null) | ||
{ | ||
review.VerifiedPurchaser = (bool)jo["Approved"]; | ||
} | ||
|
||
review.CacheId = (int)jo["CacheId"]; | ||
review.Id = (int)jo["Id"]; | ||
review.Location = jo["Location"] != null ? (string)jo["Location"] : null; | ||
review.ProductId = (string)jo["ProductId"]; | ||
review.Rating = (int)jo["Rating"]; | ||
review.ReviewDateTime = jo["ReviewDateTime"] != null ? (string)jo["ReviewDateTime"] : null; | ||
review.ReviewerName = jo["ReviewerName"] != null ? (string)jo["ReviewerName"] : null; | ||
review.ShopperId = jo["ShopperId"] != null ? (string)jo["ShopperId"] : null; | ||
review.Sku = jo["Sku"] != null ? (string)jo["Sku"] : null; | ||
|
||
if (jo["Text"] != null) | ||
{ | ||
JToken reviewText = jo["Text"]; | ||
review.Text = EscapeForJson(reviewText.ToString()); | ||
Console.WriteLine($"review.Text ======== {review.Text}"); | ||
} | ||
|
||
if (jo["Title"] != null) | ||
{ | ||
JToken titleText = jo["Title"]; | ||
review.Title = EscapeForJson(titleText.ToString()); | ||
} | ||
|
||
if (jo["VerifiedPurchaser"] != null) | ||
{ | ||
review.VerifiedPurchaser = (bool)jo["VerifiedPurchaser"]; | ||
} | ||
|
||
return review; | ||
} | ||
|
||
public override bool CanWrite | ||
{ | ||
get { return false; } | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
static string EscapeForJson(string s) | ||
{ | ||
string quoted = JsonConvert.ToString(s); | ||
return quoted.Substring(1, quoted.Length - 2); | ||
} | ||
} | ||
} |
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,40 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace ReviewsRatings.Utils | ||
{ | ||
public static class ReviewSanitizer | ||
{ | ||
public static string SantizeReviewText(string rawReviewsText) | ||
{ | ||
int startPos = rawReviewsText.IndexOf("\"Text\":", StringComparison.OrdinalIgnoreCase); | ||
int i = 0; // just to keep it from somehow getting stuck | ||
while (startPos > 0 && i < 999) | ||
{ | ||
int endPos = rawReviewsText.IndexOf(",\"", startPos, StringComparison.OrdinalIgnoreCase); | ||
startPos = startPos + 7; | ||
string rawText = rawReviewsText.Substring(startPos, endPos+ - startPos); | ||
//Console.WriteLine($"rawtext is -[{rawText}]-"); | ||
if(!rawText.Equals("null")) | ||
{ | ||
// Remove quotes around the text field | ||
rawText = rawText.Substring(1, rawText.Length - 2); | ||
string replacementText = JsonConvert.ToString(rawText); | ||
//Console.WriteLine($"REPTEXT1 = {replacementText}"); | ||
// JsonConvert will add surrounding quotes - remove them | ||
replacementText = replacementText.Substring(1, replacementText.Length - 2); | ||
var regex = new Regex(Regex.Escape(rawText)); | ||
rawReviewsText = regex.Replace(rawReviewsText, replacementText, 1); | ||
} | ||
|
||
startPos = rawReviewsText.IndexOf("\"Text\":", endPos); | ||
i++; | ||
} | ||
|
||
return rawReviewsText; | ||
} | ||
} | ||
} |
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