Click here to Skip to main content
14,668,412 members
Home » Discussions » C#
   

C#

 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn5-Jul-20 3:27
sitebuilderLuc Pattyn5-Jul-20 3:27 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox5-Jul-20 5:45
professionalpkfox5-Jul-20 5:45 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn5-Jul-20 6:19
sitebuilderLuc Pattyn5-Jul-20 6:19 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox5-Jul-20 22:15
professionalpkfox5-Jul-20 22:15 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 2:12
sitebuilderLuc Pattyn6-Jul-20 2:12 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 3:08
professionalpkfox6-Jul-20 3:08 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 3:23
sitebuilderLuc Pattyn6-Jul-20 3:23 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 6:14
sitebuilderLuc Pattyn6-Jul-20 6:14 
Hi Pete,

while your last message keeps showing up and disappearing again, I changed your code a bit to be more concise and better match C# conventions:

using System;
using System.Collections.Generic;
using System.Text;

namespace LMS {
	public class LMSResponseParser {
		private byte[] responseBytes;
		private Dictionary<string, string> dict;

		public LMSResponseParser(byte[] responseBytes) {
			this.responseBytes=responseBytes;
		}

		public string HostName {
			get {
				return getValue("ENAME");
			}
		}

		public string UUID {
			get {
				return getValue("UUID");
			}
		}
		public string Version {
			get {
				return getValue("VERS");
			}
		}

		public int PortNumber {
			get {
				return Convert.ToInt32(getValue("PORT"));
			}
		}

		private string getValue(string key) {
			try {
				if (dict==null) parse();	// lazy execution, only parse when actually needed!
				return dict[key];
			} catch (Exception exc) {
				throw new ApplicationException(
					"Error parsing LMS response '"+responseBytes+"'", exc);
			}
		}

		private void parse() {
			dict=new Dictionary<string, string>();
			for (int i = 0; i<responseBytes.Length;) {
				string key = "";
				byte b = responseBytes[i++];
				while (b>='A'&&b<='Z') {
					key+=(char)b;
					b=responseBytes[i++];
				}
				int len = b; // At the end of the keyname is the data length
				string value = Encoding.ASCII.GetString(responseBytes, i, len);
				i+=len;
				dict[key]=value;
			}
		}
	}
}


What I changed:
- usage slightly different: create an LMSResponseParser then get its properties; no reuse, parsing the next LMSresponse will require a new parser object!
- public variables, properties, method names start with uppercase, locals/privates don't.
- class names don't start with a verb, think "object" not "action".
- property names don't start with a verb (except sometimes "Is"); think "characteristic".
(OTOH Java does not have properties, there one uses getXxx() and setXxx() methods.)
- no internal properties (such as your ResponseBytes), there was no need to make them properties.
- no advance declaration of local variables, declare on first use instead (C# isn't C !)
- lazy parsing: only parse (once) when some result is actually asked for.
- error handling: what if an invalid set of bytes is given? or one of the keywords isn't present while trying to get its value? The above code catches it all and throws an exception.

PS: I did not test, there might be minor mistakes...
PS2: my way of placing { at the right rather than on the next line isn't what most people do, however I prefer it as it is more vertically compact.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 6:56
professionalpkfox6-Jul-20 6:56 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 7:16
sitebuilderLuc Pattyn6-Jul-20 7:16 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 7:27
professionalpkfox6-Jul-20 7:27 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 7:43
sitebuilderLuc Pattyn6-Jul-20 7:43 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 8:05
professionalpkfox6-Jul-20 8:05 
QuestionIntegers to Float IEEE 754 Pin
Member 1487212830-Jun-20 16:39
MemberMember 1487212830-Jun-20 16:39 
AnswerRe: Integers to Float IEEE 754 Pin
Luc Pattyn30-Jun-20 17:29
sitebuilderLuc Pattyn30-Jun-20 17:29 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 5:44
MemberMember 148721281-Jul-20 5:44 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 5:50
sitebuilderLuc Pattyn1-Jul-20 5:50 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 6:08
MemberMember 148721281-Jul-20 6:08 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 6:12
sitebuilderLuc Pattyn1-Jul-20 6:12 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 6:18
MemberMember 148721281-Jul-20 6:18 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 8:09
MemberMember 148721281-Jul-20 8:09 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 9:29
sitebuilderLuc Pattyn1-Jul-20 9:29 
QuestionHow can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w30-Jun-20 11:14
Memberarnold_w30-Jun-20 11:14 
AnswerRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Dave Kreskowiak30-Jun-20 11:18
mveDave Kreskowiak30-Jun-20 11:18 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w30-Jun-20 11:36
Memberarnold_w30-Jun-20 11:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.