Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- open System.IO
- open System.Net
- open System.Text
- open System.Collections.Specialized
- // почтовый адрес
- let email = "natakir75@gmail.com"
- type Types =
- | OpenBrace | CloseBrace // { }
- | OpenBracket | CloseBracket // [ ]
- | Colon | Comma
- | String of string
- | Number of int
- | Boolean of bool
- | Null
- let devideJSON source =
- let rec token acc = function
- | (x :: _) as t when List.exists ((=)x) [':'; ','; ')'; ']'] -> acc, t
- | w :: t when Char.IsWhiteSpace(w) -> acc, t
- | [] -> acc, []
- | c :: t -> token (acc + (c.ToString())) t
- let rec parseString acc = function
- | '\\' :: '"' :: t -> parseString (acc + "\"") t
- | '\\' :: 'n' :: t -> parseString (acc + "\n") t
- | '"' :: t -> acc, t
- | c :: t -> parseString (acc + c.ToString()) t
- | _ -> failwith "Wrong JSON structure!"
- let rec tokenize acc = function
- | w :: t when Char.IsWhiteSpace(w) -> tokenize acc t
- | '{' :: t -> tokenize (OpenBrace :: acc) t
- | '}' :: t -> tokenize (CloseBrace :: acc) t
- | '[' :: t -> tokenize (OpenBracket :: acc) t
- | ']' :: t -> tokenize (CloseBracket :: acc) t
- | ':' :: t -> tokenize (Colon :: acc) t
- | ',' :: t -> tokenize (Comma :: acc) t
- // start of string
- | '"' :: t ->
- let s, t' = parseString "" t
- tokenize (String s :: acc) t'
- | 'n' :: 'u' :: 'l' :: 'l' :: t -> tokenize (Null :: acc) t
- | 't' :: 'r' :: 'u' :: 'e' :: t -> tokenize (Boolean true :: acc) t
- | 'f' :: 'a' :: 'l' :: 's' :: 'e' :: t -> tokenize (Boolean false :: acc) t
- // numbers remained
- | d :: t ->
- let n, t' = token (d.ToString()) t
- tokenize (Number (try Convert.ToInt32 n with e -> 0) :: acc) t'
- | [] -> List.rev acc
- | _ -> failwith "Wrong JSON structure!"
- tokenize [] source
- type JSON =
- | Object of (string * JSON) list
- | Array of JSON list
- | Number of int
- | String of string
- | Boolean of bool
- | Null
- let rec parseJSON json =
- let rec parse json =
- let rec parseArray list = function
- | CloseBracket :: t -> (Array (List.rev list)), t
- | Comma :: t -> parseArray list t
- | ob ->
- let a, t = parse ob
- parseArray (a :: list) t
- let rec parseObject list = function
- | CloseBrace :: t -> (Object (List.rev list)), t
- | Comma :: t -> parseObject list t
- | Types.String s :: Colon :: t ->
- let a, t = parse t
- parseObject ((s, a) :: list) t
- | _ -> failwith "Wrong JSON structure!"
- match json with
- | OpenBrace :: t -> parseObject [] t
- | OpenBracket :: t -> parseArray [] t
- | Types.String s :: t -> JSON.String s, t
- | Types.Number s :: t -> JSON.Number s, t
- | Types.Boolean s :: t -> JSON.Boolean s, t
- | Types.Null :: t -> JSON.Null, t
- | _ -> failwith "Wrong JSON structure!"
- match parse json with
- | res, [] -> res
- | _ -> failwith "Wrong JSON structure"
- let explode (s:string) =
- [for c in s -> c]
- let are_equal (tree1 : JSON) (tree2 : JSON) : bool =
- let rec cmppair (tr1 : string*JSON) (tr2 : string*JSON) : bool =
- if fst tr1 <> fst tr2 then false else recf (snd tr1) (snd tr2)
- and recf tr1 tr2 =
- match tr1 with
- | Number n ->
- match tr2 with
- | Number comp -> n = comp
- | _ -> false
- | Boolean n ->
- match tr2 with
- | Boolean comp -> n = comp
- | _ -> false
- | String n ->
- match tr2 with
- | String comp ->
- n = comp
- | _ -> false
- | Null ->
- match tr2 with
- | Null -> true
- | _ -> false
- | Array n ->
- match tr2 with
- | Array comp ->
- if n.Length = comp.Length then
- List.forall2 recf n comp
- else false
- | _ -> false
- | Object n ->
- match tr2 with
- | Object comp ->
- if n.Length = comp.Length then
- List.forall2 cmppair n comp
- else false
- | _ -> false
- let res = recf tree1 tree2
- res
- let firstTest = """
- {
- "a": 1
- "n": {
- "c": [{
- "18":27
- "2": {
- "2": 10
- }
- },2,4]
- }
- }
- """
- let secondTest = """
- {
- "a": 1
- "n": {
- "c": [{
- "18":27
- "2": {
- "2": 10
- }
- },2,3]
- }
- }
- """
- let ans1 = are_equal (firstTest |> explode |> devideJSON |> parseJSON) (firstTest |> explode |> devideJSON |> parseJSON)
- let ans2 = are_equal (firstTest |> explode |> devideJSON |> parseJSON) (secondTest |> explode |> devideJSON |> parseJSON)
- let addWhiteSpaces str =
- let s = explode str
- " " + (Seq.fold (fun acc x -> acc + x) "" (Seq.map (fun c -> c.ToString() + (if c = '\n' then " " else "")) s))
- let rec stringify = function
- | Number n -> n.ToString()
- | String s -> "\"" + s + "\""
- | Boolean b -> if b then "true" else "false"
- | Null -> "null"
- | Array a -> "[" + (Seq.fold (fun acc x ->
- (acc + (if acc.Equals("") then "" else ",") + (stringify x))) "" a) + "]"
- | Object list ->
- "{\n" + (addWhiteSpaces (Seq.fold (fun acc (x,y) -> (acc + "\"" + x + "\": " + (stringify y)) + "\n") "" list)) + "\n}"
- let generate random =
- let randomString random =
- let size = (Seq.item 1 random) + 1
- let s = Seq.init size (fun _ -> (Seq.item 1 random) % 26)
- Seq.fold (fun acc x -> acc + ('a' + char(x)).ToString()) "" s
- let rec generate' random depth =
- match Seq.item 1 random with
- | 0 -> JSON.Null
- | 1 -> JSON.Number (Seq.item 1 random)
- | 2 -> JSON.String (randomString random)
- | 3 -> JSON.Boolean (if (Seq.item 1 random) % 2 = 0 then true else false)
- | _ when depth > 3 -> JSON.Null // not to grow deep
- | x when x % 2 = 0 ->
- let size = Seq.item 1 random
- let s = Seq.init size (fun i -> i)
- JSON.Array [for i in s -> generate' random (depth + 1)]
- | _ ->
- let size = (Seq.item 1 random) + 1
- let s = Seq.init size (fun i -> i)
- let values = [for i in s -> generate' random (depth + 1)]
- let mutable st = new Set<string>([])
- while (Set.count st < size) do
- st <- Set.add (randomString random) st
- let strings = Set.toList st
- JSON.Object (List.map2 (fun x y -> (x, y)) strings values)
- generate' random 0
- let rand maxValue =
- let rnd = new Random(1337)
- Seq.initInfinite(fun _ -> rnd.Next(maxValue))
- //testing the generator and stringifier
- let a = rand 10
- let t = generate a;;
- stringify t;;
- let main () =
- let values = new NameValueCollection()
- values.Add("email", email)
- values.Add("content", File.ReadAllText(__SOURCE_DIRECTORY__ + @"/" + __SOURCE_FILE__))
- let client = new WebClient()
- let response = client.UploadValues(new Uri("http://91.239.142.110:13666/lab2"), values)
- let responseString = Text.Encoding.Default.GetString(response)
- printf "%A\n" responseString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement