using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class GooglePlayJson : SubtitleFormat
{
public override string Extension => ".json";
public override string Name => "Google Play json";
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder("{" + Environment.NewLine + " \"events\": [ ");
int count = 0;
foreach (var p in subtitle.Paragraphs)
{
sb.AppendLine(count > 0 ? ", {" : " {");
sb.AppendLine(" \"tStartMs\": " + p.StartTime.TotalMilliseconds.ToString(CultureInfo.InvariantCulture) + ",");
sb.AppendLine(" \"dDurationMs\": " + p.Duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture) + ",");
sb.AppendLine(" \"segs\": [ {");
sb.AppendLine(" \"utf8\": \"" + Json.EncodeJsonText(p.Text).Replace("
", "\\n") + "\"");
sb.AppendLine(" } ]");
sb.Append(" }");
count++;
}
sb.AppendLine("]");
sb.AppendLine("}");
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
foreach (string s in lines)
{
sb.Append(s);
}
string allText = sb.ToString().Trim();
if (!(allText.StartsWith("{", StringComparison.Ordinal) || allText.Contains("dDurationMs", StringComparison.Ordinal)))
{
return;
}
var parser = new JsonParser();
Dictionary dictionary;
try
{
dictionary = (Dictionary)parser.Parse(allText);
}
catch (ParserException)
{
return;
}
foreach (var k in dictionary.Keys)
{
if (k != "events" || !(dictionary[k] is List