Skip to content

Commit

Permalink
Added JSON specific conversion functions (fixes #194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Aug 19, 2018
1 parent 1fec535 commit a9f1967
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
21 changes: 17 additions & 4 deletions Build/JavaScript/AlphaTab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* alphaTab v0.9.1.0 (master)
* alphaTab v0.9.1.0 (develop)
*
* This file is part of alphaTab.
* Copyright © 2018, Daniel Kuschny and Contributors, All rights reserved.
Expand Down Expand Up @@ -20615,6 +20615,18 @@ alphaTab.model._HarmonicType.HarmonicType_Impl_.toString = function(this1) {
alphaTab.model.JsonConverter = $hx_exports["alphaTab"]["model"]["JsonConverter"] = function() {
};
alphaTab.model.JsonConverter.__name__ = ["alphaTab","model","JsonConverter"];
alphaTab.model.JsonConverter.ScoreToJson = function(score) {
var obj = alphaTab.model.JsonConverter.ScoreToJsObject(score);
return JSON.stringify(obj,function(k,v) {
if(ArrayBuffer.isView(v)) {
return Array.apply([], v);
}
return v;
});
};
alphaTab.model.JsonConverter.JsonToScore = function(json,settings) {
return alphaTab.model.JsonConverter.JsObjectToScore(alphaTab.model.JsonConverter.JsObjectToScore(JSON.parse(json),settings),null);
};
alphaTab.model.JsonConverter.ScoreToJsObject = function(score) {
var score2 = {}
alphaTab.model.Score.CopyTo(score,score2);
Expand Down Expand Up @@ -20750,7 +20762,8 @@ alphaTab.model.JsonConverter.ScoreToJsObject = function(score) {
}
return score2;
};
alphaTab.model.JsonConverter.JsObjectToScore = function(score,settings) {
alphaTab.model.JsonConverter.JsObjectToScore = function(jsObject,settings) {
var score = jsObject;
var score2 = new alphaTab.model.Score();
alphaTab.model.Score.CopyTo(score,score2);
alphaTab.model.RenderStylesheet.CopyTo(score.Stylesheet,score2.Stylesheet);
Expand Down Expand Up @@ -24766,8 +24779,8 @@ alphaTab.platform.javaScript.AlphaTabWorkerScoreRenderer.prototype = {
}
}
,Render: function(score,trackIndexes) {
score = alphaTab.model.JsonConverter.ScoreToJsObject(score);
this._worker.postMessage({ cmd : "alphaTab.render", score : score, trackIndexes : trackIndexes});
var jsObject = alphaTab.model.JsonConverter.ScoreToJsObject(score);
this._worker.postMessage({ cmd : "alphaTab.render", score : jsObject, trackIndexes : trackIndexes});
}
,add_PreRender: function(value) {
return this.PreRender = system._EventAction1.EventAction1_Impl_.add(this.PreRender,value);
Expand Down
4 changes: 2 additions & 2 deletions Build/JavaScript/AlphaTab.min.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions Source/AlphaTab.CSharp/AlphaTab.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Source/AlphaTab.JavaScript/Haxe/JSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/

using System;
using Haxe;
using Phase.Attributes;

Expand All @@ -28,5 +30,7 @@ public class Json
public static extern dynamic Parse(HaxeString text);
[Name("stringify")]
public static extern HaxeString Stringify(object value);
[Name("stringify")]
public static extern HaxeString Stringify(object value, Func<dynamic, dynamic, dynamic> replacer);
}
}
48 changes: 45 additions & 3 deletions Source/AlphaTab.JavaScript/Importer/Model/JsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
using AlphaTab.Audio.Synth.Midi.Event;
using AlphaTab.Collections;
using AlphaTab.Haxe;
using AlphaTab.Platform;
using Haxe.Js.Html;
using Phase;

namespace AlphaTab.Model
{
Expand All @@ -30,7 +31,41 @@ namespace AlphaTab.Model
/// </summary>
class JsonConverter
{
public static Score ScoreToJsObject(Score score)
/// <summary>
/// Converts the given score into a JSON encoded string.
/// </summary>
/// <param name="score">The score to serialize. </param>
/// <returns>A JSON encoded string that can be used togehter with <see cref="JSONToScore"/> for conversion.</returns>
public static string ScoreToJson(Score score)
{
var obj = ScoreToJsObject(score);
return Json.Stringify(obj, (k, v) =>
{
if (ArrayBuffer.IsView(v))
{
return Script.Write<object>("untyped __js__(\"Array.apply([], {0})\", v)");
}
return v;
});
}

/// <summary>
/// Converts the given JSON string back to a <see cref="Score"/> object.
/// </summary>
/// <param name="json">The JSON string that was created via <see cref="ScoreToJSON"/></param>
/// <param name="settings">The settings to use during conversion.</param>
/// <returns>The converted score object.</returns>
public static Score JsonToScore(string json, Settings settings = null)
{
return JsObjectToScore(JsObjectToScore(Json.Parse(json), settings));
}

/// <summary>
/// Converts the score into a JavaScript object without circular dependencies.
/// </summary>
/// <param name="score">The score object to serialize</param>
/// <returns>A serialized score object without ciruclar dependencies that can be used for further serializations.</returns>
public static object ScoreToJsObject(Score score)
{
Score score2 = Platform.Platform.NewObject();
Score.CopyTo(score, score2);
Expand Down Expand Up @@ -194,8 +229,15 @@ public static Score ScoreToJsObject(Score score)
return score2;
}

public static Score JsObjectToScore(Score score, Settings settings = null)
/// <summary>
/// Converts the given JavaScript object into a score object.
/// </summary>
/// <param name="jsObject">The javascript object created via <see cref="ScoreToJsObject"/></param>
/// <param name="settings">The settings to use during conversion.</param>
/// <returns>The converted score object.</returns>
public static Score JsObjectToScore(object jsObject, Settings settings = null)
{
Score score = jsObject.As<Score>();
var score2 = new Score();
Score.CopyTo(score, score2);
RenderStylesheet.CopyTo(score.Stylesheet, score2.Stylesheet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ private void HandleWorkerMessage(Event e)

public void Render(Score score, int[] trackIndexes)
{
score = JsonConverter.ScoreToJsObject(score);
_worker.PostMessage(new { cmd = "alphaTab.render", score = score, trackIndexes = trackIndexes });
var jsObject = JsonConverter.ScoreToJsObject(score);
_worker.PostMessage(new { cmd = "alphaTab.render", score = jsObject, trackIndexes = trackIndexes });
}

public event Action<RenderFinishedEventArgs> PreRender;
Expand Down

0 comments on commit a9f1967

Please sign in to comment.