Skip to content

Instantly share code, notes, and snippets.

@lantoli
Last active August 29, 2015 14:10
Show Gist options
  • Save lantoli/fc7119c7dd8de26ca99a to your computer and use it in GitHub Desktop.
Save lantoli/fc7119c7dd8de26ca99a to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
// http://json.codeplex.com/
// NUGET: install-package Newtonsoft.Json
// MY TIME: Millisecs = 0, Ticks = 588 (total 357 ms)
namespace Reto7
{
public class JsonSplitter
{
public static JsonSplitter SplitShowsByGenre(string json, string genre) {
return new JsonSplitter { Json = json, Genre = genre };
}
public string Json { get; private set; }
public string Genre { get; private set; }
public string Item1 {
get {
return JsonConvert.SerializeObject((JsonConvert.DeserializeObject(Json) as JArray).
Where(x => x.SelectToken("genres").Values<string>().Contains(Genre)), Formatting.None);
}
}
public string Item2 {
get {
return JsonConvert.SerializeObject((JsonConvert.DeserializeObject(Json) as JArray).
Where(x => !x.SelectToken("genres").Values<string>().Contains(Genre)), Formatting.None);
}
}
}
}
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
// http://json.codeplex.com/
// NUGET: install-package Newtonsoft.Json
// MY TIME: Millisecs = 226, Ticks = 810927 (total 237 ms)
namespace Reto7
{
public class JsonSplitter
{
public static JsonSplitter SplitShowsByGenre(string json, string genre)
{
var gr = (JsonConvert.DeserializeObject(json) as JArray).GroupBy
(x => x.SelectToken("genres").Values<string>().Contains(genre));
return new JsonSplitter
{
Item1 = JsonConvert.SerializeObject(gr.Where(x => x.Key).First(), Formatting.None),
Item2 = JsonConvert.SerializeObject(gr.Where(x => !x.Key).First(), Formatting.None),
};
}
public string Item1 { get; private set; }
public string Item2 { get; private set; }
}
}
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
// MY TIME: Millisecs = 61, Ticks = 220120 (total 72 ms)
namespace Reto7
{
public class JsonSplitter
{
public static JsonSplitter SplitShowsByGenre(string json, string genre)
{
json = DecodeUnicodeChars(json);
var find = '"' + genre + '"';
var str1 = new StringBuilder();
var str2 = new StringBuilder();
int firstPosShow = -1, lastPosShow = -1;
while (json[lastPosShow + 1] != ']') {
firstPosShow = lastPosShow + 2; // skip [{ or ,{
lastPosShow = json.IndexOf('}', json.IndexOf('}', json.IndexOf('}', lastPosShow + 1) + 1) + 1); // skip images, rating, show itself
var genreIni = json.IndexOf('[', firstPosShow + 1); // genre is the only array
var genreIndex = json.IndexOf(find, genreIni + 1);
var choose = (genreIndex != -1 && genreIndex < json.IndexOf(']', genreIni + 1)) ? str1 : str2;
choose.Append(choose.Length == 0 ? '[' : ',');
choose.Append(json, firstPosShow, lastPosShow - firstPosShow + 1);
}
return new JsonSplitter {
Item1 = str1.Append(']').ToString(),
Item2 = str2.Append(']').ToString()
};
}
private static Regex _regex = new Regex(@"\\u(?<Value>[a-zA-Z0-9]{4})", RegexOptions.Compiled);
public static string DecodeUnicodeChars(string value) {
return _regex.Replace(
value,
m => ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString()
);
}
public string Item1 { get; private set; }
public string Item2 { get; private set; }
}
}
using System.Text;
namespace Reto7
{
public class JsonSplitter
{
public static JsonSplitter SplitShowsByGenre(string json, string genre) {
json = DecodeUnicodeChars(json);
var find = '"' + genre + '"';
var str1 = new StringBuilder();
var str2 = new StringBuilder();
int firstPosShow = -1, lastPosShow = -1;
while (json[lastPosShow + 1] != ']') {
firstPosShow = lastPosShow + 2; // skip ,{
lastPosShow = json.IndexOf('}', json.IndexOf('}', json.IndexOf('}', lastPosShow + 1) + 1) + 1); // skip images, rating, show itself
var genreIni = json.IndexOf('[', firstPosShow + 1); // genre is the only array
var genreIndex = json.IndexOf(find, genreIni + 1);
var choose = (genreIndex != -1 && genreIndex < json.IndexOf(']', genreIni + 1)) ? str1 : str2;
choose.Append(choose.Length == 0 ? '[' : ',');
choose.Append(json, firstPosShow, lastPosShow - firstPosShow + 1);
}
return new JsonSplitter {
Item1 = str1.Append(']').ToString(),
Item2 = str2.Append(']').ToString()
};
}
private static string DecodeUnicodeChars(string str) {
var ret = new StringBuilder();
int pos = -1, lastPos = 0;
while ( (pos = str.IndexOf("\\u", lastPos)) != -1) {
ret.Append(str, lastPos, pos - lastPos); // don't include \
ret.Append((char)(HexNumber(str[pos + 2]) << 12 | HexNumber(str[pos + 3]) << 8 |
HexNumber(str[pos + 4]) << 4 | HexNumber(str[pos + 5])));
lastPos = pos + 6;
}
return ret.Append(str, lastPos, str.Length - lastPos ).ToString();
}
private static int HexNumber(char letter) {
return (letter <= '9') ? letter - '0' : letter - 'a' + 10;
}
public string Item1 { get; private set; }
public string Item2 { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment