7

Came across this in a part of a web service I inherited from a junior we fired last year.

I am no C# expert, but I do believe there are better ways to read a byte array from a JSON string, am I right?

Comments
  • 1
    I think not the reader's fault, probably the writer somehow produce byte array as [1,2, 3 ,..] etc,

    and if the reader says he wants a byte[], the json library would expect something like a base64 string instead
  • 2
    I know nothing about c# but why is the variable Token capitalized?
  • 3
    @Pickman

    Don't ask. I only know the lady who wrote the code was fired partly due to my recommendation
  • 3
    Just convert it directly into byte array
  • 3
    @devTea @FuckJava
    What Tea said. It seems better.
  • 1
    @Pickman actually I have a problem reading js byte array, what the equivalent of both int8 and uint8 on c#? Since there’s no int8 on c#. Normally I sent a base64
  • 3
    I’m confused as why you are processing json like xml. I just turn it into a class and deal with the object returned by JsonConvert.DeseralizeObject<T>(json)
  • 2
    @bkwilliams
    Not me
    Mrs Junior who was fired
  • 2
    Correct use of ++ at least?
  • 2
    @Pickman public properties of classes in C# are capitalized by convention. Ah, but after writing this initially, I see the to line. So, not typical to have a local variable capitalized.
  • 1
    @Root

    Line 1528: No space after Token in string.

    Line 1529: How the hell is it even evaluated?

    Line 1536: Space before colon but not after it

    Line 1536: Unnecessary typecast to byte

    Do I need to go on?

    Oh last but not least, this was copy/pasted 15 times in the file. I guess back then you couldn't write a function in C# LoL
  • 2
    @FuckJava Hence "at least."

    Definitely looks like junior-level code.
  • 1
    @devTea as said before I know essentially nothing of C# but I guess that byte and sbyte are supposed to be the equivalent types. Of course if it's for a real project I would check the manual to make sure of the proper implementation of the encoding (it looks straightforward, but you never know...).
  • 2
    @devTea No int8 in C#? If with int8 you mean 8 bit signed integer, well, there's an equivalent in C#. It's called sbyte, or byte if you want it unsigned.
  • 2
    I'd say you can use JsonConvert.DeserializeObject<byte[]>(Token.ToString()) or even Token.Value<byte[]>()
  • 2
    @CodeMasterAlex it throws error when I try to pass to the webapi, I also pass as one of json object
  • 3
    @devTea Too bad I don't know your actual code etc. I use it a lot in web api and works well. Probably it's one of those small overlooked things...
  • 1
    @CodeMasterAlex thanks for your tip anyway, I’ll try it when I have to refactored my code later
Add Comment