Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1532

best VB6 json parsing method(ScriptControl+Json2.js)

$
0
0
Json-SpeedTest(Time:ms)1.3M Size data
why ChilkatJsonObject quickly than cConstructor(rc5json)?
Json-AB :Json Load string(parsing)+stringify
Action Chilkat Rc5Json Json2.js
JsonLoad 20.09 48.37 25.53
stringify 12.04 35.75 171.96
Json-AB 32.13 84.12 197.48

Name:  0190-JSON算法测速结果PK2.png
Views: 75
Size:  46.7 KB
Code:

Sub Test_ChilkatJsonObject()
'Visual Basic 6.0 Load JSON Data at Path
'https://www.example-code.com/vb6/json_load_to_path.asp

Dim p As String
p = "{""a"": 'sssss', ""b"": 2, ""c"": { ""x"": 1, ""y"": 2 } }"

Dim json As New ChilkatJsonObject
Dim success As Long
success = json.Load(p)
json.EmitCompact = 0
Debug.Print json.Emit()

Dim q As String
q = "{""mm"": 11, ""nn"": 22}"

Dim c As ChilkatJsonObject
Set c = json.ObjectOf("c")
success = c.Load(q)

'  See that x and y are replaced with mm and nn.
Debug.Print json.Emit()

End Sub

Code:

Htm = "{""a"":""test"",""b"":""abc"",""arr1"":[{""c"":""aa"",""d"":""bb""},{""e"":""dd""}]}"

Json1Code = "var JSON=function(){var m={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','""':'\\""','\\':'\\\\'},s={'boolean':function(x){return String(x)},number:function(x){return isFinite(x)?String(x):'null'},string:function(x){if(/[""\\\x00-\x1f]/.test(x)){x=x.replace(/([\x00-\x1f\\""])/g,function(a,b){var c=m[b];if(c){return c}c=b.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16)})}return'""'+x+'""'},object:function(x){if(x){var a=[],b,f,i,l,v;if(x instanceof Array){a[0]='[';l=x.length;for(i=0;i<l;i+=1){v=x[i];f=s[typeof v];if(f){v=f(v);if(typeof v=='string'){if(b){a[a.length]=','}a[a.length]=v;b=true}}}a[a.length]=']'}else if(x instanceof Object){a[0]='{';for(i in x){v=x[i];f=s[typeof v];if(f){v=f(v);if(typeof v=='string'){if(b){a[a.length]=','}a.push(s.string(i),':',v);b=true}}}a[a.length]='}'}else{return}return a.join('')}return'null'}};return{"
Json1Code = Json1Code & "copyright: '(c)2005 JSON.org',license:'http://www.crockford.com/JSON/license.html',stringify:function(v){var f=s[typeof v];if(f){v=f(v);if(typeof v=='string'){return v}}return null},parse:function(text){try{return!(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/""(\\.|[^""\\])*""/g,'')))&&eval('('+text+')')}catch(e){return false}}}}();"

'json2_stringify.js
Json1Code = "if(typeof JSON!=='object'){JSON={}}(function(){'use strict';var rx_one=/^[\],:{}\s]*$/;var rx_two=/\\(?:[""\\\/bfnrt]|u[0-9a-fA-F]{4})/g;var rx_three=/""[^""\\\n\r]*""|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;var rx_four=/(?:^|:|,)(?:\s*\[)+/g;var rx_escapable=/[\\""\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;var rx_dangerous=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;function f(n){return(n<10)?'0'+n:n}function this_value(){return this.valueOf()}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(){return isFinite(this.valueOf())?(this.getUTCFullYear()+'-'+f(this.getUTCMonth()+1)+'-'+f(this.getUTCDate())+'T'+f(this.getUTCHours())+':'+f(this.getUTCMinutes())+':'+f(this.getUTCSeconds())+'Z'):null};Boolean.prototype.toJSON=this_value;Number.prototype.toJSON=this_value;String.prototype.toJSON=this_value}" _
& "var gap;var indent;var meta;var rep;function quote(string){rx_escapable.lastIndex=0;return rx_escapable.test(string)?'\""'+string.replace(rx_escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})+'\""':'\""'+string+'\""'}function str(key,holder){var i;var k;var v;var length;var mind=gap;var partial;var value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key)}if(typeof rep==='function'){value=rep.call(holder,key,value)}switch(typeof value){case'string':return quote(value);case'number':return(isFinite(value))?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null'}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null'}v=partial.length===0?'[]':gap?('[\n'+gap+partial.join(',\n'+gap)+'\n'+mind+']'):'['+partial.join(',')+']'" _
& ";gap=mind;return v} if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){if(typeof rep[i]==='string'){k=rep[i];v=str(k,value);if(v){partial.push(quote(k)+((gap)?': ':':')+v)}}}}else{for(k in value){if(Object.prototype.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+((gap)?': ':':')+v)}}}}v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+mind+'}':'{'+partial.join(',')+'}';gap=mind;return v}}if(typeof JSON.stringify!=='function'){meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','\""':'\\\""','\\':'\\\\'};JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' '}}else if(typeof space==='string'){indent=space}rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}return str('',{'':value})}}}());"


Dim Js As ScriptControl, JsonStr As String
Set Js = New ScriptControl
Js.Language = "Javascript"
Js.AddCode "var JsonObj=" & Htm '字符转成json对象

Js.AddCode Json1Code
JsonStr = Js.Eval("JSON.stringify(JsonObj)")  'JSON对象转成“字符串”
MsgBox "JsonStr=" & JsonStr

cConstructor ,RC5-JSON(cConstructor vbRichClient5.DLL)
Code:

Htm = "{""a"":""中国人"",""b"":""abc"",""arr1"":[{""c"":""aa"",""d"":""bb""},{""e"":""dd""}]}"

Dim New_c As New cConstructor
Dim oJson As cCollection
Dim sJSONout As String

Set oJson = New_c.JSONDecodeToCollection(Htm)  'decode the JSON-string to Object
sJSONout = oJson.SerializeToJSONString 'serialize the Object back into a JSON-string

json.js(1.2k)
json2.js (2.84k)
json3.js(8k)

i test json2.js is the best(used time :16 ms,JsonBag,38ms)
---------------------------
In the case of large files, I eventually lost to the JS object
After all, that is native JS, VB still has to parse

50K JSON string to object, object to character
Json1, time: 18.162882
Json2, time: 16.407462
Json3, time: 41.214192
vbjson, time: 125.604048
JsonConverter, time: 40.81495
cDataJSON, time: 48.869498
JsonBag, time: 38.097204
---------------------------

50K large file, Json object to string
Json1, time: 28.468988
Json2, time: 17.008122
Json3, time: 38.91001
vbjson, time: 83.524864
JsonConverter, time: 18.332476
cDataJSON, time: 7.14318
JsonBag, time: 21.876478
Attached Images
 

Viewing all articles
Browse latest Browse all 1532

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>