fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

Add GetString function

Open goodspark opened this issue 5 years ago • 6 comments
trafficstars

Resolves #53

Convenience function so users don't have to do:

s := string(v.GetStringBytes("key", "a", "b"))

goodspark avatar Mar 02 '20 19:03 goodspark

Codecov Report

Merging #54 into master will increase coverage by 0.01%. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #54      +/-   ##
==========================================
+ Coverage   97.00%   97.01%   +0.01%     
==========================================
  Files           9        9              
  Lines        1067     1072       +5     
==========================================
+ Hits         1035     1040       +5     
  Misses         19       19              
  Partials       13       13              

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1649ca5...da55489. Read the comment docs.

codecov[bot] avatar Mar 02 '20 19:03 codecov[bot]

Unfortunately this won't work as expected, since the returned string will contain garbage after the next call to Parser.Parse, since v.s is backed by byte slice, which is modified on each Parser.Parse call.

valyala avatar Mar 03 '20 21:03 valyala

What do you suggest?

goodspark avatar Mar 03 '20 21:03 goodspark

Either stick to string(v.GetStringBytes()) or use something unsafe like toUnsafeString(v.GetStringBytes()) if you know possible consequences of improper usage of the returned unsafe string.

valyala avatar Mar 03 '20 22:03 valyala

What if GetString returned string(v.GetStringBytes())?

goodspark avatar Mar 03 '20 22:03 goodspark

string(v.GetStringBytes()) will do a memory copy. I think add "GetString" is not a good idea, we shall keep GetStringBytes and let users choose use b2s() or string() in their usecases.

phuslu avatar Oct 23 '20 03:10 phuslu